Skip to main content

Random Numbers In Squirrel

Functions For Generating Pseudo-random Numbers

Squirrel’s math library provides the rand() method for generating random numbers. The value returned lies somewhere between 0 and the system constant RAND_MAX, which equals 2147483647. This is usually a far wider range of values than you need, so the following code provides functions that will return respectively an integer or a floating point value between zero and a maximum value you supply.

In the case of imp001 through imp004m, the value generated generated by math.rand() is the output of the host STMicroelectronics SM32 SoC’s high-quality random number generation hardware, which has passed National Institute of Standards and Technology SP800-22b tests. It is effectively a true random number generator. On the imp005, which is based on a Broadcom SoC, math.rand() instead uses a pseudorandom generator fed with entropy from a conventional collection of timing-related and phase-related sources.

Example Code

Squirrel Methods Used