naev 0.12.6
rng.c File Reference

Handles all the random number logic. More...

#include "rng.h"
Include dependency graph for rng.c:

Go to the source code of this file.

Macros

#define LOW   0.02425
#define HIGH   0.97575

Functions

static uint32_t rng_timeEntropy (void)
 Uses time as a source of entropy.
static void mt_initArray (uint32_t seed)
 Generates the initial mersenne twister based on seed.
static void mt_genArray (void)
 Generates a new set of random numbers for the mersenne twister.
static uint32_t mt_getInt (void)
 Gets the next int.
void rng_init (void)
 Initializes the random subsystem.
unsigned int randint (void)
 Gets a random integer.
double randfp (void)
 Gets a random float between 0 and 1 (inclusive).
double Normal (double x)
 Calculates the Normal distribution.
double NormalInverse (double p)
 Calculates the inverse of the normal.

Variables

static uint32_t MT [624]
static uint32_t mt_y
static int mt_pos = 0
static double m_div = (double)( 0xFFFFFFFF )
static const double a []
static const double b []
static const double c []
static const double d []

Detailed Description

Handles all the random number logic.

Random numbers are currently generated using the mersenne twister.

Definition in file rng.c.

Macro Definition Documentation

◆ HIGH

#define HIGH   0.97575

High area threshold.

Definition at line 267 of file rng.c.

◆ LOW

#define LOW   0.02425

Low area threshold.

Definition at line 266 of file rng.c.

Function Documentation

◆ mt_genArray()

void mt_genArray ( void )
static

Generates a new set of random numbers for the mersenne twister.

Definition at line 125 of file rng.c.

◆ mt_getInt()

uint32_t mt_getInt ( void )
static

Gets the next int.

Returns
A random 4 byte number.

Definition at line 144 of file rng.c.

◆ mt_initArray()

void mt_initArray ( uint32_t seed)
static

Generates the initial mersenne twister based on seed.

Definition at line 112 of file rng.c.

◆ Normal()

double Normal ( double x)

Calculates the Normal distribution.

Calculates N(x) where N is the normal distribution.

Approximates to a power series:

N(x) = 1 - n(x)*(b1*t + b2*t^2 + b3*t^3 + b4*t^4 + b5*t^5) + Err where t = 1 / (1 + 0.2316419*x)

Maximum absolute error is 7.5e^-8.

Parameters
xValue to calculate the normal of.
Returns
The value of the Normal.

Definition at line 201 of file rng.c.

◆ NormalInverse()

double NormalInverse ( double p)

Calculates the inverse of the normal.

Lower tail quantile for standard normal distribution function.

This function returns an approximation of the inverse cumulative standard normal distribution function. I.e., given P, it returns an approximation to the X satisfying P = Pr{Z <= X} where Z is a random variable from the standard normal distribution.

The algorithm uses a minimax approximation by rational functions and the result has a relative error whose absolute value is less than 1.15e-9.

Author: Peter J. Acklam Time-stamp: 2002-06-09 18:45:44 +0200 E-mail: jackl.nosp@m.am@m.nosp@m.ath.u.nosp@m.io.n.nosp@m.o WWW URL: http://www.math.uio.no/~jacklam

C implementation adapted from Peter's Perl version.

Original algorithm from http://home.online.no/~pjacklam/notes/invnorm/ .

Definition at line 268 of file rng.c.

◆ randfp()

double randfp ( void )

Gets a random float between 0 and 1 (inclusive).

Returns
A random float between 0 and 1 (inclusive).

Definition at line 178 of file rng.c.

◆ randint()

unsigned int randint ( void )

Gets a random integer.

Returns
A random integer.

Definition at line 165 of file rng.c.

◆ rng_init()

void rng_init ( void )

Initializes the random subsystem.

Definition at line 53 of file rng.c.

◆ rng_timeEntropy()

uint32_t rng_timeEntropy ( void )
static

Uses time as a source of entropy.

Returns
A 4 byte entropy seed.

Definition at line 90 of file rng.c.

Variable Documentation

◆ a

const double a[]
static
Initial value:
= {
-3.969683028665376e+01,
2.209460984245205e+02,
-2.759285104469687e+02,
1.383577518672690e+02,
-3.066479806614716e+01,
2.506628277459239e+00 }

Inverse normal coefficients.

Definition at line 245 of file rng.c.

◆ b

const double b[]
static
Initial value:
= {
-5.447609879822406e+01, 1.615858368580409e+02, -1.556989798598866e+02,
6.680131188771972e+01,
-1.328068155288572e+01 }

Inverse normal coefficients.

Definition at line 252 of file rng.c.

◆ c

const double c[]
static
Initial value:
= {
-7.784894002430293e-03,
-3.223964580411365e-01,
-2.400758277161838e+00,
-2.549732539343734e+00,
4.374664141464968e+00,
2.938163982698783e+00 }

Inverse normal coefficients.

Definition at line 256 of file rng.c.

◆ d

const double d[]
static
Initial value:
= {
7.784695709041462e-03, 3.224671290700398e-01, 2.445134137142996e+00,
3.754408661907416e+00 }

Inverse normal coefficients.

Definition at line 263 of file rng.c.

◆ m_div

double m_div = (double)( 0xFFFFFFFF )
static

Number to divide by.

Definition at line 177 of file rng.c.

◆ MT

uint32_t MT[624]
static

Mersenne twister state.

Definition at line 35 of file rng.c.

◆ mt_pos

int mt_pos = 0
static

Current number being used.

Definition at line 37 of file rng.c.

◆ mt_y

uint32_t mt_y
static

Internal mersenne twister variable.

Definition at line 36 of file rng.c.