How do I go about producing random numbers within a range?
The srand() function is used to set the starting value for the series of random integers. You can use the srand() to set the seed of the rand function to a different starting point. The parameters that are passed into the srand() function are the starting values for the rand method.
The rand function, declared in stdlib. h, returns a random integer in the range 0 to RAND_MAX (inclusive) every time you call it. On machines using the GNU C library RAND_MAX is equal to INT_MAX or 231-1, but it may be as small as 32767.
You can try
Random r = new Random(); int rInt = r.Next(0, 100); //for ints int range = 100; double rDouble = r.NextDouble()* range; //for doubles
Have a look at
Random Class, Random.Next Method (Int32, Int32) and Random.NextDouble Method
Try below code.
Random rnd = new Random(); int month = rnd.Next(1, 13); // creates a number between 1 and 12 int dice = rnd.Next(1, 7); // creates a number between 1 and 6 int card = rnd.Next(52); // creates a number between 0 and 51
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With