Rand - Pseudorandom number generators

Description

Pseudorandom number generators

Various implementations of pseudorandom number generators (PRNG). The return value is always a number between 0.0 and 1.0.

OVERVIEW

Rand_Abstract:
This library can be used to implement pseudorandom number generators in your project. It contains various implementations of pseudorandom number generators (PRNG). The return value is always a number between 0.0 and 1.0.

Interfaces: No hardware interface required
Rand_How_to_use:
/* Global variables for the most recently calculated pseudorandom numbers */ new Float:fRand_xor; // PRN calculated using Xorshift (32bit) algorithm new Float:fRand_vb6; // PRN calculated using rnd algorithm of VB6 new Float:fRand_mwc; // PRN calculated using Multiply-with-carry algorithm new Float:fRand_crc; // PRN calculated using CRC32 algorithm /* 1 sec. timer is used for the general program sequence */ #callback MainTimer() { /* Calculates the PRNs using the different methods. The seed is not reinitialized. The calculation result of the last function call is used as new starting value */ fRand_xor = rand_xor(); fRand_vb6 = rand_vb6(); fRand_mwc = rand_mwc(); fRand_crc = rand_crc(); // Issues the most recently calculated PRNs via the watch panel #watch("xor = %f",fRand_xor); #watch("vb6 = %f",fRand_vb6); #watch("mwc = %f",fRand_mwc); #watch("crc = %f",fRand_crc); } /** * Function that is called up when the device has been prepared for the start of the application by the * "salve" function. I.e. the device is ready for proceeding with the application initialization */ #callback appInit() { /* Calculates the PRNs using the different methods. For all methods, the seed is (re)initialized with system time. */ fRand_xor = rand_xor(true); fRand_vb6 = rand_vb6(true); fRand_mwc = rand_mwc(true); fRand_crc = rand_crc(true); // Issues the most recently calculated PRNs via the watch panel #watch("xor = %f",fRand_xor); #watch("vb6 = %f",fRand_vb6); #watch("mwc = %f",fRand_mwc); #watch("crc = %f",fRand_crc); // Initialisation of a cyclic sec. timer setInterval(MainTimer, 1000); } /* Application entry point */ main() { salve( appInit); // Prepares device for application start }

BASIC

Float:rand_xor(bInit = false)

PRN calculation by using Xorshift (32bit) algorithm


A PRN between 0.0 and 1.0 is calculated using the Xorshift (32bit) algorithm. A static variable contains the starting value for the Xorshift. If no (re)initialisation should be performed, the calculation result of the last function call is used as new starting value when calling the function again (static variable). The initialisation value should be a true random number.

bInit : s32 - Determines the initial value
false - Seed will not be (re)initialised with system time (default)
true - Seed will be (re)initialised with system time
returns : Float - PRN between 0.0 and 1.0
Float:rand_vb6(bInit = false)

PRN calculation by using rnd algorithm of VB6


A PRN between 0.0 and 1.0 is calculated using the rnd algorithm of VB6. A static variable contains the starting value. If no (re)initialisation should be performed, the calculation result of the last function call is used as new starting value when calling the function again (static variable). The initialisation value should be a true random number.

bInit : s32 - Determines the initial value
false - Seed will not be (re)initialised with system time (default)
true - Seed will be (re)initialised with system time
returns : Float - PRN between 0.0 and 1.0
Float:rand_mwc(bInit = false)

PRN calculation by using Multiply-with-carry algorithm


A PRN between 0.0 and 1.0 is calculated using George Marsaglia's MWC algorithm to produce an unsigned integer. Two static variables that contain the seeds are separately used within the algorithm and the output results directly from them. The two partial results are used as initial values when calling the function again (static variables). The initialisation value of one seed should be a true random number.

bInit : s32 - Determines the initial value
false - Seed will not be (re)initialised with system time (default)
true - Seed will be (re)initialised with system time
returns : Float - PRN between 0.0 and 1.0
Float:rand_crc(bInit = false)

PRN calculation by using CRC32 algorithm


A PRN between 0.0 and 1.0 is calculated using the CRC32 algorithm. A static variable contains the starting value for the CRC32. If no (re)initialisation should be performed, the calculation result of the last function call is used as new starting value when calling the function again (static variable). The system function CRC32() requires an array of s32 values (cells) as parameter input hence the initialisation value needs to be a s32 type cell array. The initialisation value should be a true random number.

bInit : s32 - Determines the initial value
false - Seed will not be (re)initialised with system time (default)
true - Seed will be (re)initialised with system time
returns : Float - PRN between 0.0 and 1.0

On this page

Rand - Pseudorandom number generators