This is an implementation in C for linear congruential generator that has this formula: $$ X_{n+1}=a*X_{n} \bmod m $$ Below are two versions of the linear congruential generator function, the first function generates a 64-bit integer number and the second one should generate a double.
The Modulus in the codes below is 2ˆ64−1. I want to rewrite the modulus to be 2^64, when I tried that I get an error because of the data type. Since 2^64 is a 65 bit and the function has a "uint64_t" data type that holds 64 bits. I looked for solutions to solve the problem like using a 128 bits data type but I am using Xcode on Mac and it does not support it, and some recommend using GPM but I don't prefer it. I thought about storing the Modulus in an array but I don't know how I can later get the final output to be a 64-bit integer.
Is there any simple way to do it? because I need the final output from the first function to be a 64-bit integer and the output from the second function to be a double, so later I can use them in further calculations.
uint64_t linear_congruential()
{
uint64_t s= 1442695040888963407;// seed
unsigned long long int m=18446744073709551615; // The Modulus 2ˆ64−1
uint64_t a=6364136223846793005; // the multiplier a
s=(s * a )&m;
return s;
}
The second function that has double:
double linear_congruential_d()
{
double q;
uint64_t s= 1442695040888963407; //seed
unsigned long long int m=18446744073709551615; // The Modulus 2ˆ64−1
uint64_t a=6364136223846793005 ; // the multiplier a
s=(s * a )&m;
q=s/m;
return q;
}
Aucun commentaire:
Enregistrer un commentaire