Random Number Generation
[Framework]

This module generates random numbers. More...

Collaboration diagram for Random Number Generation:


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).

Detailed Description

This module generates random numbers.

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;
Unfortunately this tends to drastically destroy the distribution of the numbers, which can have unexpected results when used with network protocols. For example the following graph shows the result of taking an otherwise perfectly flat distribution between 0-32768, then subjecting it to % 20000:
dist_mod_20000.png
Notice how much more likely smaller numbers are (almost twice as likely). Results change depending on the value of X in rand() % X.

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).


Function Documentation

uint16_t fip_rand16 ( void   ) 

Get a random number between uniformly distributed between 0 to UINT16_MAX.

Returns:
random number

References fip_random_number16.

uint16_t fip_rand16Limited ( uint16_t  max  ) 

Get a random number between uniformly distributed between 0 to max.

Parameters:
max Maximum value of random number returned, max of UINT16_MAX
Returns:
random number

References fip_random_number16.

uint32_t fip_rand32 ( void   ) 

Get a random number between uniformly distributed between 0 to UINT32_MAX.

Returns:
random number

References fip_random_number32.

uint32_t fip_rand32Limited ( uint32_t  max  ) 

Get a random number between uniformly distributed between 0 to max.

Parameters:
max Maximum value of random number returned, max of UINT32_MAX
Returns:
random number

References fip_random_number32.

uint8_t fip_rand8 ( void   ) 

Get a random number between uniformly distributed between 0 to UINT8_MAX.

Returns:
random number

References fip_random_number8.

uint8_t fip_rand8Limited ( uint8_t  max  ) 

Get a random number between uniformly distributed between 0 to max.

Parameters:
max Maximum value of random number returned, max of UINT8_MAX
Returns:
random number

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.

Parameters:
seed 

References fip_random_number.


Generated on Wed Dec 8 13:00:31 2010 for FIP by  doxygen 1.5.7.1