Previous Page Next Page Contents

random -- generate random numbers

Introduction

random() returns a random integer number.

random(n1..n2) returns a procedure that generates random integers between n1 and n2.

Call(s)

random()
random(n1..n2)
random(n)

Parameters

n1, n2 - integers with n1 <= n2
n - a positive integer

Returns

random() returns a nonnegative integer. The calls random(n1..n2) and random(n) return a procedure of type DOM_PROC.

Side Effects

random as well as the random number generators created by it are sensitive to the environment variable SEED.

Related Functions

Details

Example 1

The following call produces a sequence of random integers. Note that an index variable i must be used in the construction of the sequence. A call such as random() $ 8 would produce 8 copies of the same random value:

>> random() $ i = 1..8
      427419669081, 321110693270, 343633073697, 474256143563,
      
         558458718976, 746753830538, 32062222085, 722974121768

The following call produces a ``die'' that is rolled 20 times:

>> die := random(1..6): die() $ i = 1..20
        2, 2, 2, 4, 4, 3, 3, 2, 1, 4, 4, 6, 1, 1, 1, 2, 4, 2, 1, 3

The following call produces a ``coin'' that produces ``head'' or ``tail'':

>> coin := random(2): coin() $ i = 1..10
                       1, 0, 1, 1, 0, 1, 0, 1, 0, 0
>> subs(%, [0 = head, 1 = tail])
        tail, head, tail, tail, head, tail, head, tail, head, head

The following call produces a generator of uniformly distributed floating point numbers between -1.0 and 1.0:

>> r := random(-10^DIGITS..10^DIGITS)/float(10^DIGITS):
>> r() $ i = 1..12;
      0.1905754559, 0.2075358358, 0.5537108789, 0.1638155425,
      
         0.2610874287, -0.7132768677, -0.7457691643, 0.9053675583,
      
         -0.4759211428, 0.1898567228, 0.6881793744, -0.9192271682
>> delete dice, coin, r:

Example 2

random is sensitive to the global variable SEED which is set and reset when MuPAD is (re-)initialized. The seed may also be set by the user. Random sequences can be reproduced by starting with a fixed SEED:

>> SEED := 1: random() $ i = 1..4
          427419669081, 321110693270, 343633073697, 474256143563
>> SEED := 1: random() $ i = 1..4
          427419669081, 321110693270, 343633073697, 474256143563

Example 3

random allows to create several random number generators for different ranges of numbers, and to use them simultaneously:

>> r1 := random(0..4): r2 := random(2..9): [r1(), r2()] $ i = 1..6
              [1, 4], [0, 2], [1, 3], [0, 5], [2, 2], [4, 7]
>> delete r1, r2:

Background

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000