Class

Random


Description

Used to generate Random numbers.

Properties

Name

Type

Read-Only

Shared

Seed

Double

Methods

Name

Parameters

Returns

Shared

Gaussian

Double

InRange

minR As Integer, maxR As Integer

Integer

LessThan

Range As Integer

Integer

Number

Double

RandomizeSeed

Property descriptions


Random.Seed

Seed As Double

Gets or sets the seed used by the Random number generator.

Use the Seed to initialize the pseudo-Random number generator's algorithm so that you can always get the same stream of numbers from the same seed. However, this is not an endian-independent operation. If you set the seed on a big-endian machine to the same value that you used on a little-endian machine, you will get two different streams of pseudo-Random numbers.

This example initializes the seed for to global Random to a new value:

System.Random.Seed = 17327

Method descriptions


Random.Gaussian

Gaussian As Double

Returns a Random number drawn from a Gaussian (a.k.a. Normal) distribution rather than from a uniform distribution.

The Gaussian distribution has a mean of zero and a standard deviation of 1.

This example generates a Random number from a Normal distribution.

Var d As Double = System.Random.Gaussian

Random.InRange

InRange(minR As Integer, maxR As Integer) As Integer

Returns a Random number as an Integer in the range from minR to maxR, inclusive.

The following example generates a Random integer in the range from 0 to 1,000.

Label1.Text = System.Random.InRange(0, 1000).ToString

Random.LessThan

LessThan(Range As Integer) As Integer

Returns a Random number as an Integer in the range greater than or equal to 0 and less than Range (value >= 0 And value < Range).

This example generates a Random integer from 0 to 99:

Label1.Text = System.Random.LessThan(100).ToString

Random.Number

Number As Double

Returns a Random number as a Double in the range greater than or equal to 0 and less than 1.

This example generates a Random number in the interval >= 0 and < 1.

Var d As Double
d = System.Random.Number
Label1.Text = d.ToString

Random.RandomizeSeed

RandomizeSeed

Changes the Random object's seed value to start a new sequence of pseudo Random numbers.

You might use this when you want to re-initialize a Random object but don't want to create an entirely new one. If you are creating multiple sequences, this helps to make the sequences unrelated to one another.

This example assumes that the method created a sequence from an earlier seed.

' create other sequences from another seed here..
System.Random.RandomizeSeed
Label1.Text = System.Random.Gaussian.ToString

Notes

Use System.Random to create an instance of this class.

The Random class supersedes the Rnd function. It performs the same functions and more.

A custom Random number generator is used. For best results, you should only create a single instance of Random per app.

The Random class's seed is used to initialize the pseudo-Random number generator's algorithm so that you can always get the same stream of numbers from the same seed. However, this is not an endian-independent operation. If you set the seed on a big-endian machine to the same value that you used on a little-endian machine, you will get two different streams of pseudo-Random numbers.

Compatibility

All project types on all supported operating systems.

See also

Object parent class; Rnd, System.Random functions