Files | |
file | fip_rand.c |
Random Number Generation. | |
Functions | |
void | fip_srand (uint32_t seed) |
Seed the random number with a certain seed. | |
uint8_t | fip_rand8Limited (uint8_t max) |
Get a random number between uniformly distributed between 0 to max. | |
uint8_t | fip_rand8 (void) |
Get a random number between uniformly distributed between 0 to UINT8_MAX. | |
uint16_t | fip_rand16Limited (uint16_t max) |
Get a random number between uniformly distributed between 0 to max. | |
uint16_t | fip_rand16 (void) |
Get a random number between uniformly distributed between 0 to UINT16_MAX. | |
uint32_t | fip_rand32Limited (uint32_t max) |
Get a random number between uniformly distributed between 0 to max. | |
uint32_t | fip_rand32 (void) |
Get a random number between uniformly distributed between 0 to UINT32_MAX. | |
Variables | |
static uint32_t | fip_random_number = 1u |
This number stores the 32-bit random number in use on the system. | |
static uint32_t *const | fip_random_number32 = (uint32_t *)&fip_random_number |
Access the 32-bit random number as a 32-bit number (for read only). | |
static uint16_t *const | fip_random_number16 = (uint16_t *)&fip_random_number |
Access the 32-bit random number as a 16-bit number (for read only). | |
static uint8_t *const | fip_random_number8 = (uint8_t *)&fip_random_number |
Access the 32-bit random number as a 8-bit number (for read only). |
More importantly it does a good job of generating random numbers between user-defined spans, which is often required in network protocols, but is mostly done wrong. For example to scale a random number between 0 - 20000, you might just use something like:
randNumber = rand() % 20000;
Using the functions in this module instead result in the expected distribution. Various functions are provided to provide a whole range of random number sizes in both limited (where the MAX value is user-defined) and normal (where the MAX value is defined based on the maximum value that can be represented in an integer of a certain bit-size).
uint16_t fip_rand16 | ( | void | ) |
Get a random number between uniformly distributed between 0 to UINT16_MAX.
References fip_random_number16.
uint16_t fip_rand16Limited | ( | uint16_t | max | ) |
Get a random number between uniformly distributed between 0 to max.
max | Maximum value of random number returned, max of UINT16_MAX |
References fip_random_number16.
uint32_t fip_rand32 | ( | void | ) |
Get a random number between uniformly distributed between 0 to UINT32_MAX.
References fip_random_number32.
uint32_t fip_rand32Limited | ( | uint32_t | max | ) |
Get a random number between uniformly distributed between 0 to max.
max | Maximum value of random number returned, max of UINT32_MAX |
References fip_random_number32.
uint8_t fip_rand8 | ( | void | ) |
Get a random number between uniformly distributed between 0 to UINT8_MAX.
References fip_random_number8.
uint8_t fip_rand8Limited | ( | uint8_t | max | ) |
Get a random number between uniformly distributed between 0 to max.
max | Maximum value of random number returned, max of UINT8_MAX |
References fip_random_number8.
void fip_srand | ( | uint32_t | seed | ) |
Seed the random number with a certain seed.
Must be called before any of the random functions are used.
seed |
References fip_random_number.