> <Sorry this question is not tied to C#, but I could not find a good
> forum that tackles such questions>
>
> Hi
> I am trying to come up with a good hashing function for user IDs in my
> app. The user IDs are always of the form 0,1,2,3........
> I don't know the total number of users beforehand. I am essentially
> trying to write the following method:
> // Given a Hash Percent, return 'true' if user ID lies between 0 and
> that, 'false' otherwise.
> bool IsInRange(int userId, double hashPercent);
> So, if 0,1,2,3..199 are fed to this function with hashPercent as 10.5,
> the function should return 'true' for 21 users.
Of course, if you know nothing about the number of users, you cannot hit
exactly 21 users.
But how about using the userId as seed for Random?
bool IsInRange(int userId, double hashPercent)
{
return new Random(userId).NextDouble() < hashPercent;
}
Here, you rely on different seeds giving sufficiently different values.
>
> I tried out doing some math on the numbers using mod but it did not
> work. is there an easy way to do this?
>
> Thanks
> Bruce
|