C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: The first argument to Next() is the inclusive minimum number allowed by the random number generator.
Argument 2: This argument is an exclusive maximum. So it never occurs in the output—all numbers must be lower.
C# program that uses Random
using System;
class Program
{
static void Main()
{
// ... Create new Random object.
Random random = new Random();
// ... Get 3 random numbers.
// These are always 5, 6, 7, 8 or 9.
Console.WriteLine("RANDOM: " + random.Next(5, 10));
Console.WriteLine("RANDOM: " + random.Next(5, 10));
Console.WriteLine("RANDOM: " + random.Next(5, 10));
}
}
Output
RANDOM: 6
RANDOM: 5
RANDOM: 9
Tip: If we created a new Random at the method level as a local, a problem would arise. The time-dependent seed would repeat itself.
C# program that uses static Random
using System;
class Program
{
static void Main()
{
// Call a method that uses class-level Random.
UseStatic();
// Call the same method.
// ... The random number sequence will still be random.
UseStatic();
}
static Random _random = new Random();
static void UseStatic()
{
// Use class-level Random.
// ... When this method is called many times, it still has good Randoms.
int result = _random.Next();
// If this method declared a local Random, it would repeat itself.
Console.WriteLine("STATIC RANDOM: " + result);
}
}
Output
STATIC RANDOM: 810380956
STATIC RANDOM: 123622001
So: If we create many Random objects at the same time (or close in time) our random numbers will repeat.
Tip: Use a field or pass a Random as an argument to avoid repeated random numbers.
C# program that causes repeated random numbers
class Program
{
static void Main()
{
Test();
Test();
Test();
}
static void Test()
{
// Create random and use it.
// ... This is time-dependent, so can repeat if called many times.
var random = new System.Random();
System.Console.WriteLine("CURRENT: " + random.Next());
}
}
Output
CURRENT: 747025912
CURRENT: 747025912
CURRENT: 747025912
C# program that shows Random exception
using System;
class Program
{
static void Main()
{
var random = new Random();
// First argument must be lower than the second.
int result = random.Next(20, 10);
}
}
Output
Unhandled Exception: System.ArgumentOutOfRangeException:
'minValue' cannot be greater than maxValue.
Parameter name: minValue
at System.Random.Next(Int32 minValue, Int32 maxValue)
at Program.Main()...
Important: We will not get the actual parameter argument we passed, as the top is exclusive.
Also: Another option is to pass the Next() method 2 parameters. The lower bound is inclusive, but the upper one is not.
C# program that uses Random with parameter
using System;
class Program
{
static void Main()
{
F();
F();
F();
F();
F();
F();
}
static Random _r = new Random();
static void F()
{
int n = _r.Next(5);
// Can return 0, 1, 2, 3, or 4.
Console.WriteLine(n);
}
}
Output
2
1
1
0
3
2
Note: Using arguments to Next() that are in the correct range would be better than using modulo, as the code is more direct and simpler.
However: If we wish to use a single random number in multiple ways, modulo division can help here.
Tip: If the array length is 3, and we have a random number and use modulo division by 3, we get the values 0, 1 and 2.
C# program that uses modulo with Random
using System;
class Program
{
static void Main()
{
int[] array = new int[3];
var random = new Random();
// Get 10 random numbers, then use modulo to increment array elements.
for (int i = 0; i < 10; i++)
{
int result = random.Next(0, 100);
int modulo = result % array.Length;
array[modulo]++;
}
// Display the end result.
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("ARRAY {0} = {1}", i, array[i]);
}
}
}
Output
ARRAY 0 = 2
ARRAY 1 = 4
ARRAY 2 = 4
OrderBy: The program uses the LINQ method syntax, which combines the OrderBy with a lambda expression.
OrderBy, OrderByDescendingResult: ToArray converts the enumerable letters in the statement to a string again, producing the final result: a randomized string.
ToArrayString ConstructorC# program that randomizes strings
using System;
using System.Linq;
class Program
{
static void Main()
{
// The source string.
const string original = "senators";
// The random number sequence.
Random num = new Random();
// Create new string from the reordered char array.
string rand = new string(original.ToCharArray().
OrderBy(s => (num.Next(2) % 2) == 0).ToArray());
// Write results.
Console.WriteLine("original: {0}\r\nrand: {1}",
original,
rand);
Console.Read();
}
}
Output
original: senators
rand: tossenar
Info: The NextBytes method allows you to get a random value of arbitrary length in one method invocation.
Tip: While an integer is only 4 bytes, you can use NextBytes to get much more random data at once.
C# program that uses NextBytes method
using System;
class Program
{
static void Main()
{
// Put random bytes into this array.
byte[] array = new byte[8];
// Use Random class and NextBytes method.
// ... Display the bytes with the following method.
Random random = new Random();
random.NextBytes(array);
Display(array);
random.NextBytes(array);
Display(array);
}
static void Display(byte[] array)
{
// Loop through and display bytes in the array.
foreach (byte value in array)
{
Console.Write(value);
Console.Write(' ');
}
Console.WriteLine();
}
}
Output
177 209 137 61 204 127 103 88
167 246 80 251 252 204 35 239
And: With a modulo expression, we can index those ints and use them in place of a call to Random.Next.
Version 1: This version of the code repeatedly invokes the Random.Next method to get random ints.
Version 2: This version uses a List element access of precomputed random ints. This approach might work in some programs.
Result: It is faster to access precomputed ints. We could even use a static list and avoid the startup time cost.
C# program that times cache for Random ints
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
static Random _random = new Random();
static List<int> _randomCache = new List<int>();
static void AddRandoms()
{
// Add random numbers to a list for later use.
for (int i = 0; i < 1000; i++)
{
_randomCache.Add(_random.Next());
}
}
const int _max = 10000000;
static void Main()
{
// Precompute some random ints.
AddRandoms();
// Version 1: get new random int.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int random = _random.Next();
if (random < 0)
{
return;
}
}
s1.Stop();
// Version 2: use cached random int from list.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int random = _randomCache[i % _randomCache.Count];
if (random < 0)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
8.93 ns Random.Next()
2.79 ns Random Cache (List)
Instead: A random number generator returns sufficiently random (random-appearing) numbers.
Int, uintImportant: This means that if the time is the same when 2 Randoms are created, you have a problem.
Quote: The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to improve randomness is to make the seed value time-dependent.
Random Class: Microsoft DocsOne random: We should "create one Random to generate many random numbers over time." This can be done with a local variable or field.
State: In object-oriented programming, we think of classes as machines that must store state.
And: The Random() object has state, which is dependent on time. It is not stateless.
Thus: It is not ideal to create multiple Randoms when you can use only one. With one, we improve randomness and performance.
Note: The RNG stands for random number generator. The name is confusing because of its abbreviation.