Skip to content
Snippets Groups Projects
random_pcg.c 955 B
Newer Older
  • Learn to ignore specific revisions
  • Branislav Jansik's avatar
    Branislav Jansik committed
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #define MB 262144
    
    typedef struct { uint64_t state;  uint64_t inc; } pcg32_random_t;
    
    uint32_t pcg32_random_r(pcg32_random_t* rng)
    {
        uint64_t oldstate = rng->state;
        // Advance internal state
        rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1);
        // Calculate output function (XSH RR), uses old state for max ILP
        uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
        uint32_t rot = oldstate >> 59u;
        return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
    }
    
    
    int main(int argc, char **argv)
    {
    int j,jmax=0;
    
    if (argc >1) jmax = atoi(argv[1]);
    
    #pragma omp parallel for default(none) shared(jmax,stdout) private(j) schedule(dynamic,1)
    for (j=0; j<jmax; j++)
    {
        uint32_t rndval[MB];
        int i;
        pcg32_random_t rng;
    
        for(i=0; i<MB; i+=1)
        rndval[i]=pcg32_random_r(&rng);
    
    #pragma omp critical
        fwrite(rndval,4,MB,stdout);
    }
    
    return(0);
    }