![]() |
School of Computer Science and Engineering, UNSW |
CRICOS Provider No. 00098G |
|
Last updated 28.09.06 |
9.5: Random NumbersThe k r ( Given a positive numeric prefix argument M, it produces a random integer N in the range 0 <= N < M. Each of the M values appears with equal probability. With no numeric prefix argument, the k r command takes its argument from the stack instead. Once again, if this is a positive integer M the result is a random integer less than M. However, note that while numeric prefix arguments are limited to six digits or so, an M taken from the stack can be arbitrarily large. If M is negative, the result is a random integer in the range M < N <= 0. If the value on the stack is a floating-point number M, the result is a random floating-point number N in the range 0 <= N < M or M < N <= 0, according to the sign of M. If M is zero, the result is a Gaussian-distributed random real number; the distribution has a mean of zero and a standard deviation of one. The algorithm used generates random numbers in pairs; thus, every other call to this function will be especially fast. If M is an error form ` If M is an interval form, the lower and upper bounds specify the acceptable limits of the random numbers. If both bounds are integers, the result is a random integer in the specified range. If either bound is floating-point, the result is a random real number in the specified range. If the interval is open at either end, the result will be sure not to equal that end value. (This makes a big difference for integer intervals, but for floating-point intervals it's relatively minor: with a precision of 6, ` If M is a vector, the result is one element taken at random from the vector. All elements of the vector are given equal probabilities. The sequence of numbers produced by k r is completely random by default, i.e., the sequence is seeded each time you start Calc using the current time and other information. You can get a reproducible sequence by storing a particular ``seed value'' in the Calc variable The The k a ( The k h ( If M is a floating-point number, zero, or an error form (so that the random values are being drawn from the set of real numbers) there is little practical difference between using k h and using k r several times. But if the set of possible values consists of just a few integers, or the elements of a vector, then there is a very real chance that multiple k r's will produce the same number more than once. The k h command produces a vector whose elements are always distinct. (Actually, there is a slight exception: If M is a vector, no given vector element will be drawn more than once, but if several elements of M are equal, they may each make it into the result vector.) One use of k h is to rearrange a list at random. This happens if the prefix argument is equal to the number of values in the list: [1, 1.5, 2, 2.5, 3] 5 k h might produce the permuted list ` To do the equivalent of k h but with duplications allowed, given M on the stack and with n just entered as a numeric prefix, use v b to build a vector of copies of M, then use V M k r to ``map'' the normal k r function over the elements of this vector. See Matrix Functions.
|
|
|