By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

How to generate an eight digit random number?

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
How can I generate an eight digit random?


Random r = new Random();
....
int num = r.Next(10000000, 100000000);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

P: n/a
Henry wrote:
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...nneTwister.asp
http://www.c-sharpcorner.com/Code/20...wisterAlgo.asp

--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.