The Math Library: 1.3 Basic Math Functions: Random Number Generation

Up: GEOS SDK TechDocs | Up | Prev: 1.2 Transcendental Functions | Next: 2 Conversions to Other Types
 FloatRandomize(),  FloatRandom(),  FloatRandomN()

The Math Library also provides routines to create random numbers. Using any of these routines requires that you manually push and pop numbers on the FP stack.

FloatRandomize() primes the random number generator, in preparation for a call to FloatRandom() or FloatRandomN() . If FloatRandomize() is passed the flag RGIF_USE_SEED, the routine must also pass a developer supplied seed.

FloatRandom() returns a random value between 0 (inclusive) and 1 (exclusive). The number is placed on top of the FP stack. To assign that value to a variable, use FloatPopNumber() .

FloatRandomN() returns a random value between 0 (inclusive) and N (exclusive), where N is an integer. The integer value must be on top of the FP stack. The returned integer is pushed onto the FP stack.To assign that value to a variable, use FloatPopNumber() .

Code Display D-3 Creating a Random Number

/*
 * This method takes a passed seed (passedSeed) and returns a random integer 
 * between 0 and 100.
 */
@method MyProcessClass, MSG_GET_RANDOM_FLOAT {
    long double			randomX;
	/* Prime the random number generator. */
    FloatRandomize(RGIF_USE_SEED, passedSeed);
    randomX = 100;
    FloatPushNumber(&randomX);
    FloatRandomN();
    FloatPopNumber(&randomX);
    return(randomX);
}

Up: GEOS SDK TechDocs | Up | Prev: 1.2 Transcendental Functions | Next: 2 Conversions to Other Types