开发者

Matching a classic ASP random number with a C# random number

开发者 https://www.devze.com 2023-03-18 21:56 出处:网络
Bit of a strange question.I have a website which has some pages in classic ASP, and others in ASP.net.

Bit of a strange question. I have a website which has some pages in classic ASP, and others in ASP.net.

I have a script that caches their gravatar image. This is hosted on a cookieless domain, in one of 开发者_开发技巧the following locations:

http://static1.scirra.net
http://static2.scirra.net
http://static3.scirra.net
http://static4.scirra.net

When a page requests a gravatar on my ASP.net site, it passes through this function which distributes it randomly to a static server:

/// <summary>
/// Returns the static url for gravatar
/// </summary>
public static string GetGravatarURL(string Hash, int Size, int AuthorID)
{
    Random rndNum = new Random(AuthorID);
    int ServerID = rndNum.Next(0, 4)+1;

    string R = "//static" + ServerID.ToString() + ".scirra.net/avatars/" + Size + "/" + Hash + ".png";
    return R;
}

The function in my Classic ASP parts of the website is:

function ShowGravatar(Hash, AuthorID)

    Dim ServerID

    Randomize(AuthorID)
    ServerID = Int((Rnd * 4) + 1)

    ShowGravatar = "//static" & ServerID & ".scirra.net/avatars/" & intGravatarSize & "/" & Hash & ".png"

end function

It works fine, it seeds on the users ID then assigns them a static server to server their avatars from. The only problem is, the C# and Classic ASP RNG's output different results! This is not optimum for caching as the same image is being served on up to 2 different domains.

Any easy way around this?


A random number generator that returns a predictable value is called a hash - predictable randomness is not cool at all in a random number generator :-)

So, replace the call to rand by some hash function and you're all set. Use your imagination: the hash function could be something as simple as the modulo 4 of the crc of the authorid.


Why don't you just use the gravatar hash to determine the server? For example you could take the first char of the gravatar hash modulo 4.

0

精彩评论

暂无评论...
验证码 换一张
取 消