coco_random.c File Reference

Definitions of functions regarding COCO random numbers. More...

#include <math.h>
#include "coco.h"
#include <stdio.h>

Data Structures

struct  coco_random_state_s
 A structure containing the state of the COCO random generator. More...
 

Macros

#define COCO_NORMAL_POLAR   /* Use polar transformation method */
 
#define COCO_SHORT_LAG   273
 
#define COCO_LONG_LAG   607
 

Functions

static void coco_random_generate (coco_random_state_t *state)
 A lagged Fibonacci random number generator. More...
 
coco_random_state_tcoco_random_new (uint32_t seed)
 Creates and returns a new random number state using the given seed. More...
 
void coco_random_free (coco_random_state_t *state)
 Frees all memory associated with the random state. More...
 
double coco_random_uniform (coco_random_state_t *state)
 Returns one uniform [0, 1) random value from the random number generator associated with the given state. More...
 
double coco_random_normal (coco_random_state_t *state)
 Generates an approximately normal random number. More...
 

Detailed Description

Definitions of functions regarding COCO random numbers.

Note
This file contains non-C89-standard types (such as uint32_t and uint64_t), which should eventually be fixed.

Macro Definition Documentation

#define COCO_LONG_LAG   607
#define COCO_NORMAL_POLAR   /* Use polar transformation method */
#define COCO_SHORT_LAG   273

Function Documentation

void coco_random_free ( coco_random_state_t state)

Frees all memory associated with the random state.

static void coco_random_generate ( coco_random_state_t state)
static

A lagged Fibonacci random number generator.

This generator is nice because it is reasonably small and directly generates double values. The chosen lags (607 and 273) lead to a generator with a period in excess of 2^607-1.

coco_random_state_t* coco_random_new ( uint32_t  seed)

Creates and returns a new random number state using the given seed.

double coco_random_normal ( coco_random_state_t state)

Generates an approximately normal random number.

Instead of using the (expensive) polar method, we may cheat and abuse the central limit theorem. The sum of 12 uniform random values has mean 6, variance 1 and is approximately normal. Subtract 6 and you get an approximately N(0, 1) random number.

double coco_random_uniform ( coco_random_state_t state)

Returns one uniform [0, 1) random value from the random number generator associated with the given state.