31

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.

Sam
  • 7,252
  • 16
  • 46
  • 65
Seth Taddiken
  • 574
  • 1
  • 5
  • 18

4 Answers4

32

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);
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • @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. – Jon Skeet Nov 24 '12 at 10:07
  • What would a variable look like that if i printed it out was a random number between 1 and 100? – Seth Taddiken Nov 24 '12 at 10:16
  • Would making the 100 a 101 in "(r.Next(100))" make 100 inclusive? – Seth Taddiken Nov 24 '12 at 10:25
  • @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 Nov 24 '12 at 10:36
  • 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 – Zbigniew Nov 24 '12 at 10:41
24

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Could i just say something like "Random random = new Random.Next(0, 101);"? – Seth Taddiken Nov 24 '12 at 09:41
  • 2
    @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 Nov 24 '12 at 09:42
  • 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. – Danrex Jan 12 '14 at 00:40
  • @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. – Darek Apr 09 '14 at 04:02
0

I've been searching the internet for RNG for a while now. Everything I saw was either TOO complex or was just not what I was looking for. After reading a few articles I was able to come up with this simple code.

{
  Random rnd = new Random(DateTime.Now.Millisecond);
  int[] b = new int[10] { 5, 8, 1, 7, 3, 2, 9, 0, 4, 6 };
  textBox1.Text = Convert.ToString(b[rnd.Next(10)])
}

Simple explanation,

  1. create a 1 dimensional integer array.
  2. full up the array with unordered numbers.
  3. use the rnd.Next to get the position of the number that will be picked.

This works well.

To obtain a random number less than 100 use

{
  Random rnd = new Random(DateTime.Now.Millisecond);
  int[] b = new int[10] { 5, 8, 1, 7, 3, 2, 9, 0, 4, 6 };
  int[] d = new int[10] { 9, 4, 7, 2, 8, 0, 5, 1, 3, 4 };
  textBox1.Text = Convert.ToString(b[rnd.Next(10)]) + Convert.ToString(d[rnd.Next(10)]);
}

and so on for 3, 4, 5, and 6 ... digit random numbers.

Hope this assists someone positively.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Arrai
  • 27
  • 1
  • 1
    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 – AaA Mar 21 '13 at 09:23
0

so thats kind of easy if you just use it like

Random random = new Random();

int answer = random.Next(0);
Jin Lee
  • 3,194
  • 12
  • 46
  • 86