Home

Built-In Classes: Random

 

Fundamentals

 

Introduction

Imagine you have a series of numbers, such these: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. Imagine you want to select one of these numbers, any of them. A number is referred to as random if it has been selected from a pool without a specific pattern to follow. For example, if you decide to select the value 17 from this list, if there was an exact reason that number was selected, then it is not considered random. In reality, it is difficult for a number to qualify as random. For this reason, most random numbers are referred to as pseudo-random.

To support the ability to create or choose a random number, the .NET Framework provides the Random class. To start, you can declare a variable of this class, using one of its two constructors. Here is an example that uses the default constructor:

open System

let rndNumbers : Random = new Random()

After creating the variable, you can start getting numbers from it. To do this, you call the Next() method, which is provided in three versions. One of the versions of this method takes no argument and its signature is:

abstract Next : unit -> int  
override Next : unit -> int

This method generates a randomly selected integer from 0 up. Here is an example:

open System

let rndNumbers : Random = new Random()
let rndNumber  : int    = rndNumbers.Next()

In the same way, you can call this version of the Next() method repeatedly to get random numbers.

Consider the following program:

open System

let rndNumbers : Random = new Random()
let rndNumber  : int    = rndNumbers.Next()

When creating a program that repeatedly gets a series of random numbers, you may (or may not) want the Random class to generate the same number over and over again. A seed is a constant value that controls whether a random generation would produce the same result every time it occurs. For example, using a seed, you can impose it upon the Random class to generate the same number every time the Next() method is called. To support the ability to use a seed, the Random class is equipped with a second constructor whose signature is:

abstract Next : 
        maxValue:int -> int  
override Next : 
        maxValue:int -> int

Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example:

open System

let rndNumbers : Random = new Random(100)
let rndNumber  : int    = rndNumbers.Next()

If you specify a seed, the Random class would generate the same number or the same sequence of numbers.

So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. Using the Random class, you can generate random positive numbers up to a maximum of your choice. To support this, the Random class is equipped with another version of the Next() method whose signature is:

abstract Next : 
        minValue:int * 
        maxValue:int -> int  
override Next : 
        minValue:int * 
        maxValue:int -> int

The argument to pass to the method determines the highest integer that can be generated by the Next() method. The method returns an integer. Here is an example that generates random numbers from 0 to 20:

The above version of the Next() method generates numbers starting at 0. If you want, you can specify the minimum and the maximum range of numbers that the Next() method must work with. To support this, the Random class is equipped with one more version of this method and that takes two arguments. Its syntax is:

abstract Next : 
        minValue:int * 
        maxValue:int -> int  
override Next : 
        minValue:int * 
        maxValue:int -> int

The first argument specifies the lowest value that can come from the range. The second argument holds the highest value that the Next() method can generate. Therefore, the method would operate between both values.

     
     
 

Home Copyright © 2009-2015, FunctionX Home