Random



-->

Definition

This random number generator (RNG) has generated some random numbers for you in the table below. Click 'More random numbers' to generate some more, click 'customize' to alter the number ranges (and text if required). For a full explanation of the nature of randomness and random numbers, click the 'Information' menu link. Free Random Video Chat that Works Like Omegle. ChatHub is a good alternative to Omegle. You can video chat with random people from all over the world. You can talk, text-chat, and communicate using webcam. It is completely free of charge. A random number generator, like the ones above, is a device that can generate one or many random numbers within a defined scope. Random number generators can be hardware based or pseudo-random number generators. Hardware based random-number generators can involve the use of a dice, a coin for flipping, or many other devices. A typical way to generate trivial pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range: 1. V1 = rand % 100; // v1 in the range 0 to 99 v2 = rand % 100 + 1; // v2 in the range 1 to 100 v3 = rand % 30 + 1985; // v3 in the range. Random definition: 1. Happening, done, or chosen by chance rather than according to a plan: 2. Strange or unusual.

Overloads

Random()

Initializes a new instance of the Random class using a default seed value.

Random(Int32)

Initializes a new instance of the Random class, using the specified seed value.

Initializes a new instance of the Random class using a default seed value.

Examples

The following example uses the parameterless constructor to instantiate three Random objects and displays a sequence of five random integers for each. If it is run on .NET Framework, 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 parameterless 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.

Remarks

In .NET Framework, the default seed value is derived from the system clock, which has finite resolution. As a result, different Random objects that are created in close succession by a call to the parameterless constructor have identical default seed values and, therefore, produce identical sets of random numbers. You can avoid this problem by using a single Random object to generate all random numbers. You can also work around it by generating your own random seed value and passing it to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.

In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator, so the previously described limitation does not apply. Different Random objects created in close succession produce different sets of random numbers in .NET Core.

Call this constructor if you want your random number generator to generate a random sequence of numbers. To generate a fixed sequence of random numbers that will be the same for different random number generators, call the Random(Int32) constructor with a fixed seed value. This Random constructor overload is frequently used when testing apps that use random numbers.

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

Initializes a new instance of the Random class, using the specified seed value.

Parameters

Seed
Int32

A number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.

Random

Examples

The following example creates Random objects with the class constructor that takes a seed parameter and generates a sequence of random integers and doubles. The example illustrates that the same sequence is generated when the Random object is created again with the constructor and seed parameter.

Number Generator App For Windows

Remarks

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers. This is often done when testing apps that rely on random number generators.

If your application requires different random number sequences, invoke this constructor repeatedly with different seed values. One way to produce a unique seed value is to make it time-dependent. For example, derive the seed value from the system clock, as the Random() overload does. However, the system clock might not have sufficient resolution to provide different invocations of this constructor with a different seed value. On the .NET Framework, this results in random number generators that generate identical sequences of pseudo-random numbers, as illustrated by the first two Random objects in the following example. To prevent this, apply an algorithm to differentiate the seed value in each invocation, or call the Thread.Sleep method to ensure that you provide each constructor with a different seed value.

Randomizer

Another option is to instantiate a single Random object that you use to generate all the random numbers in your application. This yields slightly better performance, since instantiating a random number generator is fairly expensive.