Generating Random Numbers

Posted Tue, Aug 11 2009 15:52 by Deborah Kurata

There are many uses for generating a set of random numbers, especially if you are developing a game. .NET provides a Random class just for this purpose.

To generate a set of 10 random numbers between 1 and 100, you use the Random class as follows:

In C#:

Random rand = new Random();
List<int> randomNumbers = new List<int>();

for (int i = 0; i < 10; i++)
{
    randomNumbers.Add(rand.Next(1, 101));
}

randomNumbers.ForEach(i => Debug.WriteLine(i));

In VB:

Dim rand As New Random
Dim randomNumbers As New List(Of Integer)

For i As Integer = 1 To 10
    randomNumbers.Add(rand.Next(1, 101))
Next

For Each i As Integer In randomNumbers
    Debug.WriteLine(i)
Next

In both examples, the random numbers are generated and added to a list. The contents of the list is then displayed in the debug window.

The Next method of the Random class returns a random number within the defined range, inclusive of the lower bound and exclusive of the upper bound. This means that using Next(1, 101) picks a random number from 1 to 100.

A second overload of the Next method takes only one parameter, which is the upper bound. Using Next(101) picks a random number from 0 to 100.

A third overload of the Next method takes no parameters. Using Next() picks a random number from 0 to the maximum integer value – 1.

Enjoy!

Filed under: , , ,

Comments

# re: Generating Random Numbers

Wednesday, August 12, 2009 9:31 AM by Jack Dolby

Ken Getz also has a nice article that includes generating a collection of random numbers using LINQ:

<msdn.microsoft.com/.../cc700332.aspx>

# re: Generating Random Numbers

Wednesday, August 12, 2009 10:29 AM by Jack Dolby

sorry... the trailing ">" on the url breaks it... I thought <> would prevent url wrapping.

msdn.microsoft.com/.../cc700332.aspx

# re: Generating Random Numbers

Wednesday, August 12, 2009 10:36 AM by Deborah Kurata

Hi Jack -

Thank you for visiting my blog!

Ken's code randomly orders a set of numbers, where my code randomly selects a subset of numbers. Both techniques are useful, depending on your requirements.

NOTE: If you have difficulty using the link in Jack's post, try this: msdn.microsoft.com/.../cc700332.aspx

# re: Generating Random Numbers

Thursday, August 13, 2009 7:57 AM by yemek tarifleri

Good for beginners, Thanks

# re: Generating Random Numbers

Thursday, August 13, 2009 9:08 AM by Chris

You should seed you random numbers; otherwise you'll end up with the same random numbers for every execution of the code.  The common used technique is to seed it with the number of milliseconds from the epoch:

Random rand = new Random(DateTime.Now.Ticks);

# re: Generating Random Numbers

Thursday, August 13, 2009 4:51 PM by Deborah Kurata

Hi Chris -

Thank you for visiting my blog and for your suggestion.

However, if you don't specify a seed, the Random method will define one for you so that you don't have the problem you defined. This is a quote from the documentation:

"Initializes a new instance of the Random class, using a time-dependent default seed value."

Here is the link:

msdn.microsoft.com/.../h343ddh9.aspx

Thank you for bringing up this topic!

# re: Generating Random Numbers

Friday, August 14, 2009 8:57 AM by moses

please ,how can you give a customer a unique number which consist at least 10 chars?

# re: Generating Random Numbers

Friday, August 14, 2009 12:23 PM by Deborah Kurata

Hi Moses -

I would not recommend using this random number technique to define unique keys for your customers.

Rather, you should consider either using an autonumber column on your database table or use GUIDS to produce unique Ids.

Here is a link:

msdn.microsoft.com/.../system.guid.aspx

# re: Generating Random Numbers

Monday, February 07, 2011 6:07 PM by pasta tarifleri

I always come to this site and this site has a nice love from Turkey

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: