Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Environment and Utilities /
Chapter 8 - QuickDraw GX Mathematics / QuickDraw GX Mathematics Reference
Mathematical Functions / Random Number Generation


RandomBits

You can use the RandomBits function to return a sequence of pseudorandom numbers.

unsigned long RandomBits(long count, long focus);
count
The number of bits in the number to be generated by the random number generator.
focus
The degree of clustering about the mean value.
function result
A sequence of pseudorandom numbers.
DESCRIPTION
The RandomBits function returns random numbers in the range of 0 to 2count - 1. A focus of 0 generates numbers that are uniformly distributed.

A positive value for the focus parameter generates numbers that are clustered about the mean, analogous to averaging 2focus uniform random numbers. A negative focus generates numbers that tend to avoid the mean.

If you define a value limit to be 1 << count, the result of the RandomBits function ranges from 0 to limit - 1. Its mean is (limit - 1) / 2. The mean is independent of the focus. If the focus is positive, the standard deviation of the numbers generated by the RandomBits function is approximately (0.28868 limit) / e1.41421 focus. As the focus parameter gets bigger, two things happen:

If the focus is negative, the RandomBits function result is computed as if it were positive; for results less than limit / 2, limit / 2 is added; for others, limit / 2 is subtracted. This causes the distribution to avoid the mean.

To generate a clustering of points around a given value, generate x and y offsets with

FractMultiply(radius, RandomBits(31, focus) - fract1); 
The average distance will be 0.57735 radius/e1.41421 focus.

A good way to select a value for the focus is to experiment until the desired result is achieved.

SEE ALSO
The SetRandomSeed function sets the starting number seed for the random number generator algorithm. The SetRandomSeed function is described in the next section. The GetRandomSeed function returns the current starting number seed for the random number generator algorithm. The GetRandomSeed function is described on page 8-60.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996