How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the C programming language

Table of Contents

Problem Statement

PCG32 has two unsigned 64-bit integers of internal state: Values of sequence allow 2**63 different sequences of random numbers from the same state. The algorithm is given 2 U64 inputs called seed_state, and seed_sequence. The algorithm proceeds in accordance with the following pseudocode:- Note that this an anamorphism – dual to catamorphism, and encoded in some languages as a general higher-order unfold function, dual to fold or reduce. numbers using the above. are: 2707161783 2068313097 3122475824 2211639955 3215226955

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the C programming language

This C code implements a pseudorandom number generator (PRNG) called PCG32, which stands for Permuted Congruential Generator, with a 32-bit output size. PCG32 is a high-quality PRNG that is known for its speed, randomness, and repeatability.

Here's a breakdown of the code:

  1. Header Includes:

    #include <math.h>
    #include <stdint.h>
    #include <stdio.h>

    These header files include standard math functions, integer types, and input/output functions.

  2. Constants:

    const uint64_t N = 6364136223846793005;

    N is a large 64-bit constant used as a multiplier in the PRNG algorithm.

  3. Global Variables:

    static uint64_t state = 0x853c49e6748fea9b;
    static uint64_t inc = 0xda3e39cb94b95bdb;

    state and inc are static global variables that hold the current state and increment value of the PRNG.

  4. Function pcg32_int:

    uint32_t pcg32_int() {
       // ...
    }

    This function generates a 32-bit integer using the PCG32 algorithm. It updates the state and inc variables with new values for the next call.

  5. Function pcg32_float:

    double pcg32_float() {
       // ...
    }

    This function generates a floating-point number between 0 and 1 using pcg32_int.

  6. Function pcg32_seed:

    void pcg32_seed(uint64_t seed_state, uint64_t seed_sequence) {
       // ...
    }

    This function initializes the PRNG with a user-provided seed. It sets state and inc to specific values based on the given seed values.

  7. main Function:

    • It starts by printing five random integers generated by PCG32.
    • It then seeds the PRNG with a different seed and generates 100,000 floating-point numbers between 0 and 5.
    • The program counts how many of these numbers fall into each of the five equal-sized ranges ([0, 1), [1, 2), [2, 3), [3, 4), and [4, 5)).
    • Finally, it prints the counts for each range, demonstrating the expected distribution of numbers in a pseudorandom sequence.

In summary, this program demonstrates the use of PCG32, a high-quality pseudorandom number generator, to generate both integers and floating-point numbers with good randomness and repeatability properties.

Source code in the c programming language

#include <math.h>
#include <stdint.h>
#include <stdio.h>

const uint64_t N = 6364136223846793005;

static uint64_t state = 0x853c49e6748fea9b;
static uint64_t inc = 0xda3e39cb94b95bdb;

uint32_t pcg32_int() {
    uint64_t old = state;
    state = old * N + inc;
    uint32_t shifted = (uint32_t)(((old >> 18) ^ old) >> 27);
    uint32_t rot = old >> 59;
    return (shifted >> rot) | (shifted << ((~rot + 1) & 31));
}

double pcg32_float() {
    return ((double)pcg32_int()) / (1LL << 32);
}

void pcg32_seed(uint64_t seed_state, uint64_t seed_sequence) {
    state = 0;
    inc = (seed_sequence << 1) | 1;
    pcg32_int();
    state = state + seed_state;
    pcg32_int();
}

int main() {
    int counts[5] = { 0, 0, 0, 0, 0 };
    int i;

    pcg32_seed(42, 54);
    printf("%u\n", pcg32_int());
    printf("%u\n", pcg32_int());
    printf("%u\n", pcg32_int());
    printf("%u\n", pcg32_int());
    printf("%u\n", pcg32_int());
    printf("\n");

    pcg32_seed(987654321, 1);
    for (i = 0; i < 100000; i++) {
        int j = (int)floor(pcg32_float() * 5.0);
        counts[j]++;
    }

    printf("The counts for 100,000 repetitions are:\n");
    for (i = 0; i < 5; i++) {
        printf("  %d : %d\n", i, counts[i]);
    }

    return 0;
}


  

You may also check:How to resolve the algorithm Check that file exists step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm A+B step by step in the Arturo programming language
You may also check:How to resolve the algorithm Permutations step by step in the AutoHotkey programming language