PC Review


Reply
Thread Tools Rate Thread

Algorithm to hash user IDs

 
 
Bruce
Guest
Posts: n/a
 
      18th Nov 2007
<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.

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
 
Reply With Quote
 
 
 
 
Niels Ull
Guest
Posts: n/a
 
      20th Nov 2007


> <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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Hash algorithm to detect duplicate content DotNetNewbie Microsoft C# .NET 24 7th Mar 2008 03:43 PM
hash code generating algorithm. lavu Microsoft VC .NET 5 20th Nov 2006 07:22 PM
Fixed hash algorithm in CertEnroll library Roman Sedov Windows Vista Security 0 29th Aug 2006 09:32 AM
Subject : Q) SHA(Secure Hash Algorithm) for wireless NDIS driver. Daum Microsoft Access Security 1 18th Aug 2006 10:39 AM
A .Net Trick Everyday! (subject of today: SHA1 - Secure Hash Algorithm Class) Nuri YILMAZ Microsoft Dot NET Framework 0 13th Aug 2004 12:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 AM.