Reputation: 157
I'm trying to make a code to return a normalized value for a random number. The code is the following:
// add libraries
# include <stdio.h>
# include <stdlib.h>
# include <stdint.h>
# include <inttypes.h>
# include <math.h>
//function declaration
double rand_number_f (void);
// since the compure is 64 bit, we take M as 2^64 - 1
unsigned long long M = 184467440709551615;
// choice of a (random but will be verified)
// a ≡ 3 or 5 modulo 8 ; in our case 3845694483 modulo 8 is 3
// a to be of the same order of magnitude
// sqrt ((2^64)/3845694483) = 1.11 which is between 1/10 and 10
unsigned long long a = 9448928019;
// initial seed x0
unsigned long long SEED = 41768947553;
int main(void)
{
printf("the number is : %f \n", rand_number_f());
}
// function to return random number between 0 and 1
double rand_number_f ()
{
SEED = SEED * a;
// return variable
double y;
y = (double)SEED/M;
return y;
}
However, the output is the following:
the number is : 39.520000 Program ended with exit code: 0
Why am I getting the value 39? By dividing by the Max value of a 64 bit unsigned value, shouldn't I only have the decimal part?
Upvotes: 0
Views: 106
Reputation: 782693
Your value for M
is wrong. You should use ULLONG_MAX
, which is 18446744073709551615
(you're missing 37
).
#include <limits.h>
unsigned long long M = ULLONG_MAX;
Upvotes: 3