Random Constructor
Collapse the table of content
Expand the table of content

Random Constructor ()


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 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.

Once you've instantiated the random number generator, you call individual Random methods, such as Next() or NextDouble(), to generate random numbers.

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.

public class HelloServer : MarshalByRefObject {

   public HelloServer() {
      Console.WriteLine("HelloServer activated.");

   public void SayHelloToServer(string name) {
      Console.WriteLine("Client invoked SayHelloToServer(\"{0}\").", name);

   // IsOneWay
   // Note the lack of the OneWayAttribute adornment on this method.
   public string SayHelloToServerAndWait(string name) {
      Console.WriteLine("Client invoked SayHelloToServerAndWait(\"{0}\").", name);

         "Client waiting for return? {0}",
         RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()) ? "No" : "Yes"

      return "Hi there, " + name + ".";

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft