# Random Number b/w 0 and 1 – C PROGRAM

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 $(a\times r_i+c) \mathrm{mod } m$ as being a good pseudo random number generator, where $a=1093, m=86436, c=18257$.
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 $m$ unique random numbers such that the largest random number generated is $(m-1)$. So to scale these down we just divide the numbers by $(m-1)$

The following code generates $n$ 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)
**/
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)
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: 