Random Numbers

COMP2521 20T2 - Random Numbers ♢ [0/10]
❖ Random Numbers


How can a computer generate a random number?

Software can only produce pseudo random numbers.

A generator of pseudo-random numbers might be "good enough"
COMP2521 20T2 - Random Numbers ♢ [1/10]
❖ ... Random Numbers

Ideally, we want a function random()

Using it in the code ...

int freq[10] = {0,0,0,0,0,0,0,0,0,0};
for (i = 0; i < 100000; i++) {
   int n = random() % 10;
   freq[n]++;
}

... after which each freq[i] should contain the same value (10000)

COMP2521 20T2 - Random Numbers ♢ [2/10]
❖ Linear Conguential Generators

The most widely-used pseudo random number technique:

LCG uses a recurrence relation:

Note: requires Xn to be saved between calls to the LCG function.
COMP2521 20T2 - Random Numbers ♢ [3/10]
❖ ... Linear Conguential Generators

Typical implementation of an LCG

#define a ???
#define c ???
#define m ???

unsigned int random()
{
   static unsigned int X;
   X = (a * X + c) % m;
   return X;
}

It is possible to omit m if a is large and we ignore overflows

COMP2521 20T2 - Random Numbers ♢ [4/10]
❖ ... Linear Conguential Generators

Clearly, we need to be careful in choice of a, c and m

Consider the case where  a=11,  m=31,  c=0,  X0=1

An LCG would produce the sequence:

11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14,
30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10,
17, 1, 11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16,
21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7,
15, 10, 17, 1, 11, 28, 29, 9, 6, 4, 13, ...

Observations:

COMP2521 20T2 - Random Numbers ♢ [5/10]
❖ ... Linear Conguential Generators

Now consider the case where  a=12,  m=30,  c=0,  X0=1

AN LCG would produce the sequence:

12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6,
12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6,
12, 24, 18, 6, 12, 24, 18, 6, ...

Does not produce all values in the range 1..30, and repeats with short period

To avoid scenarios like this ...

Note: same initial value for X0 always produces same sequence
COMP2521 20T2 - Random Numbers ♢ [6/10]
❖ ... Linear Conguential Generators

It is a complex task to pick good numbers

Lewis, Goodman and Miller (1969) suggested

Most systems use more complex LCG-based algorithms

COMP2521 20T2 - Random Numbers ♢ [7/10]
❖ Random Number Library


Two functions are required:

srandom(int seed) // sets its argument as the seed (X0)

random() // uses a LCG technique to generate random 
         // numbers in the range 0 .. RAND_MAX 

where the constant RAND_MAX is defined in stdlib.h

Typically,  RAND_MAX is large  (in CSE, RAND_MAX = 2147483647)

The period (before repetition) is long, approximately 16·((231)-1)

COMP2521 20T2 - Random Numbers ♢ [8/10]
❖ ... Random Number Library


A problem:

Handy if testing a program and need a repeatable "random" sequence.

But how to use srandom() to set a different seed each time?

Note: default seed is 0, if no call to srandom()
COMP2521 20T2 - Random Numbers ♢ [9/10]
❖ ... Random Number Library

Random numbers are typically generated in a specific range:

int randomRange(int lo, int hi)
{
	int n = random() % (hi-lo+1);
	return n + lo;
}


LCG is not good for applications that need very high-quality random numbers

However, LCG good enough for day-to-day use.
COMP2521 20T2 - Random Numbers ♢ [10/10]


Produced: 14 Jun 2020