Identifying and understanding random number algorithms

Click For Summary
The discussion focuses on generating random numbers according to a specific distribution using the box method, with a particular interest in the Box-Mueller method and its potential relation. A simple implementation for the function f(x) = sin(x) on the interval [0, π] is proposed, where random numbers x and y are generated, scaled, and accepted based on the condition y < f(x). The implementation example provided uses the rand() function, which is noted to be a pseudorandom number generator based on Knuth's algorithm. There is a query about the return value if the if clause is not satisfied, and the importance of the srand() function for seeding random number generation is highlighted. Additionally, the discussion references the rand48 family of functions, which utilize a linear congruential algorithm to generate pseudo-random numbers, emphasizing the underlying mechanics of these generators.
schniefen
Messages
177
Reaction score
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
What is the return value if the "if" clause is not satisfied?
 
  • Like
Likes QuantumQuest and jim mcnamara
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
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
22
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
25
Views
4K
Replies
11
Views
2K
  • · Replies 0 ·
Replies
0
Views
655
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
19
Views
2K