| Bsoft 2.0.5 Bernard's software package

Functions | Variables
random_numbers.cpp File Reference

Functions for generating random sequences. More...

`#include "Vector3.h"`
`#include "utilities.h"`

Go to the source code of this file.

## Functions
int | randseed (0)
long | get_rand_max ()
| Finds the maximum random number for a system. More...
double | irm (1.0L/get_rand_max())
long | random_seed ()
| Gets a random seed and sets the flag. More...
int | random_array_uniform_chunk (float *r, long start, long end, double min, double range)
float * | random_array_uniform (long n, double min, double max)
| Generates a series with a uniform random distribution. More...
double | random_gaussian (double avg, double stdev)
| Generates a value with a gaussian random distribution. More...
int | random_array_gaussian_chunk (float *r, long start, long end, double avg, double stdev)
float * | random_array_gaussian (long n, double avg, double stdev)
| Generates a series with a gaussian random distribution of values. More...
double | random_poisson (double avg)
| Generates a value deviating from the average based on a poisson distribution. More...
int | random_array_poisson_chunk (float *r, long start, long end, double avg)
float * | random_array_poisson (int n, double avg)
| Generates a series with a poisson random distribution of values. More...
double | random_logistical (double avg, double stdev)
| Generates a value with a logistical random distribution. More...
int | random_array_logistical_chunk (float *r, long start, long end, double avg, double stdev)
float * | random_array_logistical (long n, double avg, double stdev)
| Generates an array with a logistical random distribution. More...
Vector3< double > | vector3_random_unit_sphere ()
| Generates a random vector on the unit sphere. More...
Vector3< double > | vector3_random (const double min, const double max)
| Generates a random vector within a defined cube. More...
Vector3< double > | vector3_random (Vector3< double > min, Vector3< double > max)
| Generates a random vector within a defined cube. More...
Vector3< double > | vector3_random (const double length)
| Generates a random vector within a defined sphere. More...
Vector3< double > | vector3_random_gaussian (double avg, double stdev)
| Generates a random vector within a random gaussian-distributed length. More...
Vector3< double > | vector3_xy_random_gaussian (double avg, double stdev)
| Generates a random vector within a random gaussian-distributed length in the xy plane. More...
double | halton_number (long i, long b)
double * | halton_sequence (long n, long b)
| Generates a Halton sequence. More...

## Variables
int | verbose

## Detailed Description

Functions for generating random sequences.

Author
  Bernard Heymann

Date
  Created: 19990703
  Modified: 20151113

Definition in file random_numbers.cpp.

## Function Documentation

## ◆ get_rand_max()

long get_rand_max | ( |  | ) |

Finds the maximum random number for a system.

Returns
  long the maximum random number.

      Loops through random numbers to determine if the maximum is 2 or 4 bytes.

Definition at line 22 of file random_numbers.cpp.

## ◆ halton_number()

double halton_number | ( | long | i,
|  | long | b
| ) |  |

Definition at line 536 of file random_numbers.cpp.

## ◆ halton_sequence()

double* halton_sequence | ( | long | n,
|  | long | b
| ) |  |

Generates a Halton sequence.

Parameters

  n | length of sequence.
  b | base.

Returns
  double* sequence.

      The Halton sequence is a pseudo-random array.

Definition at line 558 of file random_numbers.cpp.

## ◆ irm()

double irm | ( | 1.0L/ | get_rand_max() | ) |

## ◆ random_array_gaussian()

float* random_array_gaussian | ( | long | n,
|  | double | avg,
|  | double | stdev
| ) |  |

Generates a series with a gaussian random distribution of values.

Parameters

  n | number of values.
  avg | average.
  stdev | standard deviation.

Returns
  float* the array of values.

      An array of floating point numbers is generated with a gaussian
      distribution with a given average and standard deviation:
          value = average + std_dev*sqrt(-2*log(random_value))*
                          cos(2*PI*random_value);
      where random_value is between 0 and 1.

Definition at line 172 of file random_numbers.cpp.

## ◆ random_array_gaussian_chunk()

int random_array_gaussian_chunk | ( | float * | r,
|  | long | start,
|  | long | end,
|  | double | avg,
|  | double | stdev
| ) |  |

Definition at line 143 of file random_numbers.cpp.

## ◆ random_array_logistical()

float* random_array_logistical | ( | long | n,
|  | double | avg,
|  | double | stdev
| ) |  |

Generates an array with a logistical random distribution.

Parameters

  n | number of values.
  avg | average.
  stdev | standard deviation.

Returns
  float* array of values.

      An array of floating point numbers is generated with a logistical
      differential distribution with a given average and standard deviation:
          value = average + (std_dev/golden)*ln(1/random_value - 1)
      where random_value is between 0 and 1 and:
          golden  = (sqrt(5) + 1)/2

  Reference: Press W.H. et al (1992) Numerical Recipes in C.

Definition at line 379 of file random_numbers.cpp.

## ◆ random_array_logistical_chunk()

int random_array_logistical_chunk | ( | float * | r,
|  | long | start,
|  | long | end,
|  | double | avg,
|  | double | stdev
| ) |  |

Definition at line 353 of file random_numbers.cpp.

## ◆ random_array_poisson()

float* random_array_poisson | ( | int | n,
|  | double | avg
| ) |  |

Generates a series with a poisson random distribution of values.

Parameters

  n | number of values.
  avg | average.

Returns
  float* the array of values.

      The poisson distribution is given for j = 0,1,... by:
                  avg^j * exp(-avg)
          P(j) = -----------------
                         j!
      Note that only positive integer values are defined for j and sum(P(j)) = 1.
      An array of floating point numbers is generated with a poisson
      distribution with a given average. The standard deviation is:
          std = sqrt(avg)
      If the average <= 0, the return array contains only zeroes.

  Reference: Press W.H. et al (1992) Numerical Recipes in C.

Definition at line 296 of file random_numbers.cpp.

## ◆ random_array_poisson_chunk()

int random_array_poisson_chunk | ( | float * | r,
|  | long | start,
|  | long | end,
|  | double | avg
| ) |  |

Definition at line 249 of file random_numbers.cpp.

## ◆ random_array_uniform()

float* random_array_uniform | ( | long | n,
|  | double | min,
|  | double | max
| ) |  |

Generates a series with a uniform random distribution.

Parameters

  n | number of values.
  min | minimum value.
  max | maximum value.

Returns
  float* array with uniform random numbers.

      An array of floating point numbers is generated distributed uniformly
      in the range of the given minimum and maximum:
          value = random_value*(max - min) + min
      where random_value is between 0 and 1.
      The average and standard deviation are:
          average = (max + min)/2
          standard deviation = 0.5*sqrt(1/3)*(max - min).

Definition at line 76 of file random_numbers.cpp.

## ◆ random_array_uniform_chunk()

int random_array_uniform_chunk | ( | float * | r,
|  | long | start,
|  | long | end,
|  | double | min,
|  | double | range
| ) |  |

Definition at line 52 of file random_numbers.cpp.

## ◆ random_gaussian()

double random_gaussian | ( | double | avg,
|  | double | stdev
| ) |  |

Generates a value with a gaussian random distribution.

Parameters

  avg | average.
  stdev | standard deviation.

Returns
  double the random value.

      A floating point number is generated with a gaussian
      distribution with a given average and standard deviation:
          value = average + std_dev*sqrt(-2*log(random_value))*
                          cos(2*PI*random_value);
      where random_value is between 0 and 1.

Definition at line 123 of file random_numbers.cpp.

## ◆ random_logistical()

double random_logistical | ( | double | avg,
|  | double | stdev
| ) |  |

Generates a value with a logistical random distribution.

Parameters

  avg | average.
  stdev | standard deviation.

Returns
  double the random value.

      A floating point number is generated with a logistical
      differential distribution with a given average and standard deviation:
          value = average + (std_dev/golden)*ln(1/random_value - 1)
      where random_value is between 0 and 1 and:
          golden  = (sqrt(5) + 1)/2

  Reference: Press W.H. et al (1992) Numerical Recipes in C.

Definition at line 338 of file random_numbers.cpp.

## ◆ random_poisson()

double random_poisson | ( | double | avg | ) |

Generates a value deviating from the average based on a poisson distribution.

Parameters

  avg | average.

Returns
  double value.

      The poisson distribution is given for j = 0,1,... by:
                  avg^j * exp(-avg)
          P(j) = -----------------
                         j!
      Note that only positive integer values are defined for j and sum(P(j)) = 1.
      A value is generated with a poisson distribution with a given average.
      If the average <= 0, the return value is zero.

  Reference: Press W.H. et al (1992) Numerical Recipes in C.

Definition at line 221 of file random_numbers.cpp.

## ◆ random_seed()

long random_seed | ( |  | ) |

Gets a random seed and sets the flag.

Returns
  int flag.

      The random seed is obtained using the program pid.
      A flag is set to prevent the seed from being generated multiple times.

Definition at line 41 of file random_numbers.cpp.

## ◆ randseed()

int randseed | ( | 0 |  | ) |

## ◆ vector3_random() [1/3]

Vector3<double> vector3_random | ( | const double | min,
|  | const double | max
| ) |  |

Generates a random vector within a defined cube.

Parameters

  min | minimum.
  max | maximum.

Returns
  Vector3<double> vector.

      Each vector element is set to a random value between the given minimum and maximum.

Definition at line 440 of file random_numbers.cpp.

## ◆ vector3_random() [2/3]

Vector3<double> vector3_random | ( | Vector3< double > | min,
|  | Vector3< double > | max
| ) |  |

Generates a random vector within a defined cube.

Parameters

  min | minimum vector.
  max | maximum vector.

Returns
  Vector3<double> vector.

      Each vector element is set to a random value between the given
      minimum and maximum vectors.

Definition at line 462 of file random_numbers.cpp.

## ◆ vector3_random() [3/3]

Vector3<double> vector3_random | ( | const double | length | ) |

Generates a random vector within a defined sphere.

Parameters

  length | maximum vector length.

Returns
  Vector3<double> vector.

      A random vector is generated, normalized and multiplied with
      a random value smaller than the given length.

Definition at line 483 of file random_numbers.cpp.

## ◆ vector3_random_gaussian()

Vector3<double> vector3_random_gaussian | ( | double | avg,
|  | double | stdev
| ) |  |

Generates a random vector within a random gaussian-distributed length.

Parameters

  avg | average of gaussian distribution.
  stdev | standard deviation of gaussian distribution.

Returns
  Vector3<double> vector.

      A random vector is generated, normalized and multiplied with
      a random value derived from a gaussian distribution.

Definition at line 502 of file random_numbers.cpp.

## ◆ vector3_random_unit_sphere()

Vector3<double> vector3_random_unit_sphere | ( |  | ) |

Generates a random vector on the unit sphere.

Returns
  Vector3<double> vector.

      A random vector is generated with a uniform distribution on the unit sphere.

  Reference: Press W.H. et al (1992) Numerical Recipes in C.

Definition at line 419 of file random_numbers.cpp.

## ◆ vector3_xy_random_gaussian()

Vector3<double> vector3_xy_random_gaussian | ( | double | avg,
|  | double | stdev
| ) |  |

Generates a random vector within a random gaussian-distributed length in the xy plane.

Parameters

  avg | average of gaussian distribution.
  stdev | standard deviation of gaussian distribution.

Returns
  Vector3<double> vector.

      A random vector is generated, the z-component set to zero, normalized
      and multiplied with a random value derived from a gaussian distribution.

Definition at line 522 of file random_numbers.cpp.

## Variable Documentation

## ◆ verbose

int verbose