Tom Gullen
Tom Gullen

Reputation: 61727

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

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?

Upvotes: 2

Views: 687

Answers (2)

CodesInChaos
CodesInChaos

Reputation: 108800

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.

Upvotes: 3

fvu
fvu

Reputation: 32953

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.

Upvotes: 3

Related Questions