I am trying to implement the PCG random number generator in c. It's kind of difficult for me to wrap my head around the examples since I am a beginner in c. So far I tried to implement the minimal version which can be found here.
Here is my code which produces now some (hopefully) pseudo random numbers:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "pcg_basic.h"
int main(int argc, char** argv){
pcg32_random_t rngptr;
pcg32_srandom_r(&rngptr, time(NULL), (intptr_t)&rngptr);
for (int round = 1; round <= 10; ++round) {
printf(" %f\n", ldexp(pcg32_random_r(&rngptr) ,-32));
}
return 0;
}
Is that even a good implementation or can this be improved better?
The problem: I want to use random numbers everywhere in my code and not only in the main() function. How can I do this? I tried:
double myrand(){
pcg32_random_t rngptr;
pcg32_srandom_r(&rngptr, time(NULL), (intptr_t)&rngptr);
return ldexp(pcg32_random_r(&rngptr) ,-32);
}
However, this approach does not work and seems wrong to me. But I don't know how to do it better.
Can someone please show me a simple approach to get good pseudo random numbers in c which work in every part of the code?
Aucun commentaire:
Enregistrer un commentaire