| ||||||||||||||||||
Random Numbers
UltimaCalc can calculate random numbers. More precisely, it contains a pseudo-random number sequence generator that generates numbers in the range 0 to 1. These numbers are 'random' in the sense that it seems to be impossible to predict what the next number will be, but they are only 'pseudo-random' because the exact same sequence may be repeated at will, if required.
To obtain a random number, use the value of the symbol random. Each time this value is obtained, it will be different.
How it works
The pseudo-random generator contains a number, called the 'seed'. The seed's value is set according to the system clock when UltimaCalc starts. When the generator runs, it performs some calculations on the seed, transforming it into a new seed. From this new seed, a decimal number is produced and presented as the result. The generator remembers the new seed, for use the next time it is called.
Obtaining the same sequence each time
The calculations performed on the seed are not random but are very carefully chosed to provide the maximum illusion of randomness. Therefore, a given seed will always give rise to the same sequence of calculations, and hence produce the same sequence of pseudo-random numbers.
To set a seed, use the function seed. For example: seed(0). The 'variable' random will then generate the values 0.82914607, 0.97825727, 0.19224099, etc. If you again evaluate seed(0), the values of random will now repeat the same sequence.
The numbers are linearly distributed over the range 0 to 1. This means, for example, that the next number is just as likely to be in the range 0 to 0.1 as it is likely to be in the range 0.45 to 0.55. The probability is 1/10 in both cases.
A different range of numbers
Often, you might wish to generate a sequence of integers, covering a specific range of values. The expression floor(a+random*b) will generate integers in the range of a to b. For example, floor(1+random*100) generates random integers in the range 1 to 100 inclusive, each value being equally likely to occur in the long run.
