In the last few posts I have been talking about random number generation using simple algorithms, and then testing if the numbers are truly random in nature or not.

In the last post we settled upon as being a good pseudo random number generator, where .

It passed the correlation test very well.

We also scaled down the random numbers so that they lie within [0,1] and plotted the distribution in various intervals between [0,1] and the frequencies were almost equal.

In this post I am going to just explain the process of scaling down the random numbers between [0,1] once again, for the sake of clarity.

So we know that the above given formula would generate unique random numbers such that the largest random number generated is . So to scale these down we just divide the numbers by

The following code generates random numbers b/w [0,1].

### CODE:

/******************************************** *********RANDOM NUMBER GENERATOR************* ***GENERATE RANDOM NUMBER BETWEEN 0 AND 1**** ********************************************/ #include<stdio.h> #include<math.h> /**Function that generates a random number. Parameters: r0: initial (first) seed a: scale factor , so that a*r0 give the first random number m: gives the max. value of random numbers that can be generated (m-1) c: additional displacement(offset) factor **/ int rand(int r0, int a, int m, int c){ int r1=(a*r0+c)%m; return r1; } /**Function that generates random numbers given a seed, and stores them in an array that is passed as an argument. Parameters: r0: initial (first) seed a: scale factor , so that a*r0 give the first random number m: gives the max. value of random numbers that can be generated (m-1) c: additional displacement factor n: no. of random numbers to be generated x[n]: array that will store the random numbers **/ void randomNos(int r0, int a, int m, int c, int n, int x[n]){ int r1=rand(r0,a,m,c);; int i; for(i=0;i<n;i++){ x[i]=r1; r1=rand(r1,a,m,c); } } main(){ int a, m, c, r0, n; printf("Enter the value of a:\n"); scanf("%d",&a); printf("Enter the value of m:\n"); scanf("%d",&m); printf("Enter the value of c:\n"); scanf("%d",&c); printf("Enter the value of r0(initial):\n"); scanf("%d",&r0); printf("Enter the no. of random nos. you require:\n"); scanf("%d",&n); int randNos[n]; randomNos(r0, a, m, c, n, randNos); //Renormalize the randomnumbers so that their values are from within [0,1] int i; double randNosNew[n]; for(i=0;i<n;i++){ randNosNew[i]=(double)randNos[i]/(m-1); } printf("The random numbers between 0 and 1 are:\n"); for(i=0;i<n;i++){ printf("%lf \n",randNosNew[i]); } }

### OUTPUT:

### References and Resources:

https://cdsmith.wordpress.com/2011/10/10/build-your-own-simple-random-numbers/

https://en.wikipedia.org/wiki/Random_number_generation

https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator

Numerical Recipes in C

I’m a physicist specializing in computational material science with a PhD in Physics from Friedrich-Schiller University Jena, Germany. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.