Identifying and understanding random number algorithms

In summary, the Box-Mueller method is a way to generate random numbers according to a distribution and the Box-Mueller algorithm is a simple implementation of this algorithm for ##f(x)=\sin{(x)}## on ##[0,\pi]##.
  • #1
schniefen
178
4
Studying different ways to generate random numbers according to a distribution and the below algorithm describes the "box method". A search on Google led to the Box-Mueller method. Are they related? Also, what would be a simple implementation of this algorithm for ##f(x)=\sin{(x)}## on ##[0,\pi]##? Is it correct that ##y## should be scaled to some value in ##[0,1]##?

We generate two random numbers ##x## and ##y##. We scale ##x## so that it
gives a random point in the restricted range we want to generate random numbers in. Now we scale
##y## so it matches the range from 0 to the maximum value of the function ##f(x)## we want to generate.
Now we accept ##x## if ##y < f(x)## and reject ##x## otherwise.​

Implementation example:
C++:
#include <math.h>

int main(){

double x = (double)rand();
double y = (double)rand();
 
double xs = fmod(x,pi);
double ys = fmod(y,1);

if (ys < sin(xs)) return sin(xs);
}
 
Technology news on Phys.org
  • #2
What is the return value if the "if" clause is not satisfied?
 
  • Like
Likes QuantumQuest and jim mcnamara
  • #3
You do realize that rand() implements a well known pseudorandom number generator in itself. The early versions of this library call is Knuth's random number generator. The Standard Library has several other PRNG's that return double data types, all positive integers (long datatype), etc.

PS: you should read about the srand() seed other seed functions...

From man page in Linux:
double drand48(void);

double erand48(unsigned short xseed[3]);

long lrand48(void);

long nrand48(unsigned short xseed[3]);

long mrand48(void);

long jrand48(unsigned short xseed[3]);

void srand48(long seed);

unsigned short *seed48(unsigned short xseed[3]);

void lcong48(unsigned short p[7]);

DESCRIPTION
The rand48 family of functions generates pseudo-random numbers using a
linear congruential algorithm working on integers 48 bits in size. The
particular formula employed is r(n+1) = (a * r(n) + c) mod m where the
default values are for the multiplicand a = 0xfdeece66d = 25214903917
and the addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48.
r(n) is called the seed of the random number generator.
 
  • Like
Likes QuantumQuest and schniefen

FAQ: Identifying and understanding random number algorithms

1. What is a random number algorithm?

A random number algorithm is a mathematical function or process used to generate a sequence of numbers that appear to be random. These numbers are often used in simulations, cryptography, and statistical sampling.

2. How do random number algorithms work?

Random number algorithms use a starting value or seed, and apply a series of mathematical operations to generate a new value. This new value then becomes the seed for the next iteration, creating a sequence of seemingly random numbers.

3. What makes a good random number algorithm?

A good random number algorithm should produce a sequence of numbers that are unpredictable, uniformly distributed, and have a long period (meaning the sequence does not repeat itself quickly). It should also be efficient and easy to implement.

4. Can random number algorithms be biased?

Yes, random number algorithms can be biased. This means that certain numbers or patterns may appear more frequently than others in the sequence. It is important to carefully select and test a random number algorithm to ensure it is truly random.

5. What are some common applications of random number algorithms?

Random number algorithms have many uses in various fields, such as simulations, games, cryptography, and scientific research. They are also commonly used in statistical sampling and generating unique identifiers or codes.

Back
Top