PLUTO  4.4-patch2
Functions
math_random.c File Reference

Miscellaneous random number generator functions. More...

#include "pluto.h"

Functions

double RandomNumber (double rmin, double rmax)
 
void RandomSeed (long int seed, long int offset)
 
double GaussianRandomNumber (double mu, double sigma)
 
double PowerLawRandomNumber (double xmin, double xmax, double n)
 
double NR_ran2 (long int *idum)
 

Detailed Description

Some useful links:

Author
A. Mignone (migno.nosp@m.ne@t.nosp@m.o.inf.nosp@m.n.it)
Date
Aug 20, 2020

Function Documentation

◆ GaussianRandomNumber()

double GaussianRandomNumber ( double  mu,
double  sigma 
)

Generate random deviates with a normal Gaussian distribution with mean mu and standard deviation sigma. (Adapted from Numerical Recipe, see "Normal (Gaussian) Deviates in Chap. 7)

◆ NR_ran2()

double NR_ran2 ( long int *  idum)

Long period (> 2 x 10^{18}) random number generator of L'Ecuyer with Bays-Durham shuffle and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate the largest floating value that is less than 1.

Example:

if (first_time){
long dummy=-34
NR_ran2(&dummy);
}
for (k = 0; k < N; k++){
printLog ("rand = %10.3e\n",NR_ran2(&dummy));
}

Reference:

  • "Numerical Recipes in C", Chap 7

◆ PowerLawRandomNumber()

double PowerLawRandomNumber ( double  xmin,
double  xmax,
double  n 
)

Generate random deviates with a power-law distribution. Here xmin and xmax are the distribution range and n is the power-law index (n != -1)

◆ RandomNumber()

double RandomNumber ( double  rmin,
double  rmax 
)

Generate and return a random number between [rmin, rmax] using the PLUTO random number generator.

Sequence must already have been seeded using RandomSeed() function (see below).

Generate a random number between a and b:

static int first_call = 1;
if (first_call){
RandomSeed(seed,offset);
first_call = 0;
}
for (k = 0; k < N; k++) {
printLog ("rand = %f\n", RandomNumber(a,b));
}

In parallel, you must ensure different sequences are generated on different processors.

  • A first possibility is to use the same seed and then to offset the sequence:
    Random_Seed(time(NULL), 32768);
    Each processor then generates the the same sequence but the starting point is offsetted by 32768*prank prns ahead.
  • A second possibility is to seed a different sequence on each processor and use the same offset:
    Random_Seed(SeedGenerator(s), 0);

◆ RandomSeed()

void RandomSeed ( long int  seed,
long int  offset 
)

Seed the random number generator. Important: the seed must be called only once per sequence.

Parameters
[in]seedlong integer used to seed the random number generator
[in]offsetlong integer specifying the offset where to start the sequence (only when offset > 0). Otherwise (offset <= 0) no offset is used.

In parallel, each processor uses a different sequence obtained by offsetting the initial one by offset*prank elements. For example, if offset = 8 and 2 processors are used, a total of 16 numbers (8 per processor) are generated. Each processor starts at a different point in the sequence:

r1  r2  r3  r4  r5  r6  r7  r8  r9  r10  r11  r12  r13  r14  r15  r16

<---------- rank 0 ---------->  <-------------- rank 1 -------------->

Example:

static int first_time = 1;
// Generate a sequence of 16384 prns (for each thread)
if (first_time) {
RandomSeed (-34, 16384);
first_time = 0;
}
r1 = RandomNumber();
.
.
.
rn = RandomNumber();