Hi everyone,
Ive been asked to do an assignment to test the randomness of a linear congruential generator..We've been given the code implemented in c..the problem is i dont really see how this code relates to the theoretical LCG equation i.e X[n+1] = (aX[n] + c) mod m..So i would be very appreciative if someone could explain the rand32() function in this code and how it implements the theoretical equation above..
many thanks
#include<stdio.h>
#include<stdint.h>
uint16_t rand16();
uint32_t rand32();
uint16_t n, userChosenValue = 0; //Change this for different seed values.
uint16_t numberOfValues = 10; //Change this depending on your needs.
uint32_t seed;
uint32_t mlcg,p,q;
uint64_t tmpseed;
int main(){
/* Calculate and print a series of 16 bit random numbers */
seed = (uint32_t)(userChosenValue + 1);
printf("16 Bit:\n\n");
for (n=0;n<numberOfValues;n++){
printf("%.4x\n",rand16());
}
/* Calculate and print a series of 32 bit random numbers */
seed = (uint32_t)(userChosenValue + 1);
printf("\n\n32 Bit:\n\n");
for (n=0;n<numberOfValues;n++){
printf("%.8x\t\t\n",rand32());
}
return 0;
}
/* Return the next 32 bit random number */
uint32_t rand32() {
tmpseed = (uint64_t)33614U * (uint64_t)seed;
q = tmpseed; /* low */
q = q >> 1;
p = tmpseed >> 32 ; /* hi */
mlcg = p + q;
if (mlcg & 0x80000000) {
mlcg = mlcg & 0x7FFFFFFF;
mlcg++;
}
seed = mlcg;
return mlcg;
}
/* Return low 16 bits of next 32 bit random number */
uint16_t rand16() {
return (uint16_t)rand32();
}