Analysis of Algorithms : An Active Learning Approach

(Ron) #1

Appendix B Pseudorandom Number Generation


Random numbers are useful in a number of computer applications. Any algo-
rithmic process we may develop, however, will not produce truly random
numbers because each time we use that same process, we will get the same set
of numbers. This is not random but can be typically made to appear random by
using a different starting point each time the program is run. This starting point
will usually depend on the computer’s system clock, which adds randomness
based on when the program is started.
One benefit to this is that the program can be tested using a consistent start-
ing point. This will generate the same sequence of numbers, making it easier to
test the program. Once the program is believed to be error free, the change to
randomize these numbers can be added.
There are a few techniques that can be used, but we will only describe one
here. The mixed congruential method has a seed value that is updated each
time a new number is needed. The new seed value is the basis for the next
pseudorandom number. The function for this is as follows:


function RanNum() returns float
seed = (seed * p + i) mod m
return seed / m
end RanNum

Free download pdf