473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to generate an eight digit random number?

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
2 17985
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
14
by: Debbie Lucier | last post by:
How would I generate a even random number each time? I can generate a random number each time but not an even one? Here is the code I use for the random number from 1-100. <script...
11
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
6
by: Brian | last post by:
I hv a school assignment that is to generate 52 different random number, where all the numbers must >1 and <53. Pls help~ Thanks~
1
by: scripter407 | last post by:
I know there are some methods like montecarlo method to test for primality but find it difficult to implement these algorithm. I would like you give your suggestions on it. Hope you all find this...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
6
by: rahulrhegde | last post by:
Generate 10 digit unique number using VBA ? I used the Unix -Time formula Now the problem with this is 2 person from different computer can generate number at same sec then this will fail...
14
by: John Crutchfiel | last post by:
Hi, I have a query that pulls information I need and lists out certain "REP ID"s. I need some sort of update script the will take the first ten and assign them all the same 8 digit number in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.