Pop-11 provides a useful (but relatively sophisticated) pseudo-random number generator, controlled by an integer variable ranseed whose value is changed whenever the generator is called. By re-setting ranseed to the same initial value (e.g. 0) one can sure that the same sequence of "random" numbers will be generated every time. If false is assigned to ranseed then an unpredictable integer value depending on the exact time of day will be assigned to it when the next random number is generated.
random0(NUM) -> RANDOMGiven a non-zero positive integer or floating-point number, this procedure generates a random number of the same type, in the range:
0 <= RANDOM < NUMwhere the distribution of RANDOM will be approximately uniform.
random(NUM) -> RANDOMSame as -random0-, except that whenever the latter would return 0 or 0.0, the original argument NUM is returned instead. It can thus be defined as
random0(NUM) -> RANDOM; if RANDOM = 0 then NUM else RANDOM endif;Hence the range of the result is
0 < RANDOM <= NUMfor a float, or
1 <= RANDOM <= NUMfor an integer.
Examples
repeat 10 times random(5) endrepeat => ** 3 5 2 1 2 1 2 4 3 3 2 -> pop_pr_places; repeat 10 times random(5.0) endrepeat => ** 0.49 4.3 1.48 3.65 0.52 4.25 2.49 0.14 2.37 3.47A closely related procedure is oneof, which takes a list and returns a randomly chosen element.
repeat 10 times oneof([1 2 3 4 5]) endrepeat => ** 5 4 5 5 4 2 4 5 3 3