How to generate an eight digit random number?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How can I generate an eight digit random? Can I use the staff name to
generate it? May I ask is there any sample c# code to see?

Thanks
 
Henry said:
How can I generate an eight digit random? Can I use the staff name to
generate it? May I ask is there any sample c# code to see?

If you wish to generate a number from a string, such that the same
string always results in the same number, you want to use a hash. This
number is not random at all, but will bear no clear relationship to its
source string, and collisions (two names mapping to the same string)
should occur extremely rarely. Depending on the application, you may
want to include a salt. Do not write your own hash; use the .NET
Framework's hashing functionality, as in this sample in C#:

http://www.obviex.com/samples/hash.aspx

If you wish to generate a random number which you don't want anyone else
to be able to guess, for security reasons, you want to use the
CryptGenRandom API, which is accessible through the
RNGCryptoServiceProvider class. Keep in mind that brute force is
effective against 8-digit numbers, though.

Finally, if you just want ordinary random numbers, for example to
control a computer player in a game, the code in Mattias's post, which
uses a linear congruential generator, will suffice. If you need
something more random and with a longer cycle but not cryptographically
strong, for example to use in a massive simulation, you might check out
these C# implementations of the Mersenne Twister algorithm:

http://www.thecodeproject.com/csharp/CsharpMersenneTwister.asp
http://www.c-sharpcorner.com/Code/2003/April/MersenneTwisterAlgo.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top