How does C#'s random number generator work?

120,641

Solution 1

I was just wondering how the random number generator in C# works.

That's implementation-specific, but the wikipedia entry for pseudo-random number generators should give you some ideas.

I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100.

You can use Random.Next(int, int):

Random rng = new Random();
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(rng.Next(1, 101));
}

Note that the upper bound is exclusive - which is why I've used 101 here.

You should also be aware of some of the "gotchas" associated with Random - in particular, you should not create a new instance every time you want to generate a random number, as otherwise if you generate lots of random numbers in a short space of time, you'll see a lot of repeats. See my article on this topic for more details.

Solution 2

You can use Random.Next(int maxValue):

Return: A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned.

var r = new Random();
// print random integer >= 0 and  < 100
Console.WriteLine(r.Next(100));

For this case however you could use Random.Next(int minValue, int maxValue), like this:

// print random integer >= 1 and < 101
Console.WriteLine(r.Next(1, 101);)
// or perhaps (if you have this specific case)
Console.WriteLine(r.Next(100) + 1);
Share:
120,641
Seth Taddiken
Author by

Seth Taddiken

Updated on March 17, 2021

Comments

  • Seth Taddiken
    Seth Taddiken about 3 years

    I was just wondering how the random number generator in C# works. I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100.

  • Seth Taddiken
    Seth Taddiken over 11 years
    Could i just say something like "Random random = new Random.Next(0, 101);"?
  • Jon Skeet
    Jon Skeet over 11 years
    @SethTaddiken: No, you can't. (And my name is Jon, not John.) Based on your questions so far today, I strongly suggest that you get hold of a good introductory C# book. It's likely to be far more effective for initial learning than asking here.
  • Jon Skeet
    Jon Skeet over 11 years
    @SethTaddiken: Well, it doesn't do what you asked for, which is a number between 1 and 100. It will never give you 100, but it will give you 0. Additionally, you should make sure you really understand that you shouldn't create a new instance of Random each time.
  • Seth Taddiken
    Seth Taddiken over 11 years
    What would a variable look like that if i printed it out was a random number between 1 and 100?
  • Seth Taddiken
    Seth Taddiken over 11 years
    Would making the 100 a 101 in "(r.Next(100))" make 100 inclusive?
  • Zbigniew
    Zbigniew over 11 years
    @SethTaddiken yes it would, please take a look at quote from MSDN and at my comment (in code). In this case @JonSkeet provided correct and more throughout answer. I've upvoted his answer because it just seems better. Personally I'd accept his answer. The ranges are exacly: value >= minValue && value < maxValue
  • Zbigniew
    Zbigniew over 11 years
    Never the less, I've edited my answer to provide answer for what you have asked for. Take a look at the MSDN, it explains it pretty well
  • AaA
    AaA about 11 years
    rnd.Next(10) already gives you a number in the range you need. you don't need an array of numbers between 1~10 to lookup
  • Danrex
    Danrex over 10 years
    Well you can be working through a book, and still need help when you are trying to play around with the different programs and information is not immediately available to you.
  • Darek
    Darek about 10 years
    @JonSkeet I know it is old thread, but I too was scratching my head recently, why Random did return not-so-random numbers. When executed in a Parallel.For() the only way to ensure close to true randomness using a static Random class, was to lock(Random){} when generating a new random number. However, I like your approach better. It is clever and provides speed of execution plus thread-safety. It was also fun to find out that one should not be worried about exhausting Int32 storage, since after reaching MaxValue, it simply flips to MinValue. Quite nice, quite nice indeed.