1 out of 10 rated this helpful - Rate this topic

Random Constructor

Updated: January 2012

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public Random()

The distribution of the generated numbers is uniform; each number is equally likely to be returned.

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.

The following example uses the default constructor to instantiate three Random objects and displays a sequence of five random integers for each. Because the first two Random objects are created in close succession, they are instantiated using identical seed values based on the system clock and, therefore, they produce an identical sequence of random numbers. On the other hand, the default constructor of the third Random object is called after a two-second delay caused by calling the Thread.Sleep method. Because this produces a different seed value for the third Random object, it produces a different sequence of random numbers.


using System;
using System.Threading;

public class RandomNumbers
{
   public static void Main()
   {
      Random rand1 = new Random();
      Random rand2 = new Random();
      Thread.Sleep(2000);
      Random rand3 = new Random();
      ShowRandomNumbers(rand1);
      ShowRandomNumbers(rand2);
      ShowRandomNumbers(rand3);
   }

   private static void ShowRandomNumbers(Random rand)
   {
      Console.WriteLine();
      byte[] values = new byte[5];
      rand.NextBytes(values);
      foreach (byte value in values)
         Console.Write("{0, 5}", value);
      Console.WriteLine();   
   }
}
// The example displays the following output to the console:
//       28   35  133  224   58
//    
//       28   35  133  224   58
//    
//       32  222   43  251   49


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

January 2012

Replaced the example.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
bubble sorter with random generator in F#

let rnd = new System.Random() // first we make the random function

let n = 2000 // then we define the length of the array and the nummber of random numbers in the array

let mutable j = n-1 // setting up for a bubble sorter

let mutable k = 0

let mutable e = (0,0)

let Swap (a, b) = (b, a) // makeing a swap function

let array1 : int array = // makeing an int array

[|

for i in 1..n -> rnd.Next():int // making the random numbers in the array
|]
while k<n do
while j>k do
if array1.[j] < array1.[j-1] then //checking if the number is bigger than the next
e <- Swap (array1.[j], array1.[j-1]) //swaping the numbers if the next number is bigger than the previous
array1.[j] <- fst e
array1.[j-1] <- snd e
j <- j-1 //moving on to the next number
k <- k+1
j <- n-1// start checking with the next set of numbers
printfn "%A" array1 // printing the array, thought limited as standart to the first 100 numbers