How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C programming language
How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C programming language
Table of Contents
Problem Statement
numbers as shown above.
are as shown above
repetitions of
Is as follows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C programming language
The provided C code defines a pseudorandom number generator (PRNG) based on the Mersenne Twister algorithm. Here's a detailed explanation:
-
Constants: The code defines several constants for the algorithm's parameters and coefficients.
-
mod() Function: The
mod()
function calculates the modulus ofx
withy
while handling negative values correctly. -
Seed Function: The
seed()
function initializes the internal state of the generators with a seed value. -
next_int() Function: The
next_int()
function generates a 32-bit pseudorandom integer using the first generator. It calculates the next value based on coefficientsa1
and the current statex1
, then updates the state for the next call. -
next_float() Function: The
next_float()
function generates a pseudorandom floating-point number between 0 and 1 by dividing the output ofnext_int()
byd
. -
Main Function: In the
main()
function:- It first prints five random integers using the default seed 1234567.
- Then, it seeds the generator with 987654321 and generates 100,000 pseudorandom floating-point numbers.
- It counts the occurrences of values falling into five ranges (0, 1, 2, 3, 4) and prints the counts.
This code implements a pseudorandom number generator that produces a sequence of seemingly random numbers. It starts with a seed value and generates subsequent numbers based on the coefficients and the internal state of the generator. The output is a sequence of 32-bit integers or floating-point numbers between 0 and 1.
Source code in the c programming language
#include <math.h>
#include <stdio.h>
#include <stdint.h>
int64_t mod(int64_t x, int64_t y) {
int64_t m = x % y;
if (m < 0) {
if (y < 0) {
return m - y;
} else {
return m + y;
}
}
return m;
}
// Constants
// First generator
const static int64_t a1[3] = { 0, 1403580, -810728 };
const static int64_t m1 = (1LL << 32) - 209;
// Second generator
const static int64_t a2[3] = { 527612, 0, -1370589 };
const static int64_t m2 = (1LL << 32) - 22853;
const static int64_t d = (1LL << 32) - 209 + 1; // m1 + 1
// the last three values of the first generator
static int64_t x1[3];
// the last three values of the second generator
static int64_t x2[3];
void seed(int64_t seed_state) {
x1[0] = seed_state;
x1[1] = 0;
x1[2] = 0;
x2[0] = seed_state;
x2[1] = 0;
x2[2] = 0;
}
int64_t next_int() {
int64_t x1i = mod((a1[0] * x1[0] + a1[1] * x1[1] + a1[2] * x1[2]), m1);
int64_t x2i = mod((a2[0] * x2[0] + a2[1] * x2[1] + a2[2] * x2[2]), m2);
int64_t z = mod(x1i - x2i, m1);
// keep last three values of the first generator
x1[2] = x1[1];
x1[1] = x1[0];
x1[0] = x1i;
// keep last three values of the second generator
x2[2] = x2[1];
x2[1] = x2[0];
x2[0] = x2i;
return z + 1;
}
double next_float() {
return (double)next_int() / d;
}
int main() {
int counts[5] = { 0, 0, 0, 0, 0 };
int i;
seed(1234567);
printf("%lld\n", next_int());
printf("%lld\n", next_int());
printf("%lld\n", next_int());
printf("%lld\n", next_int());
printf("%lld\n", next_int());
printf("\n");
seed(987654321);
for (i = 0; i < 100000; i++) {
int64_t value = floor(next_float() * 5);
counts[value]++;
}
for (i = 0; i < 5; i++) {
printf("%d: %d\n", i, counts[i]);
}
return 0;
}
You may also check:How to resolve the algorithm File size step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Inverted syntax step by step in the Ruby programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the PHP programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Phix programming language
You may also check:How to resolve the algorithm Tau function step by step in the Mathematica/Wolfram Language programming language