How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Java 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 Java programming language

The provided Java code is related to random number generation using two Linear Congruential Generators (LCGs). A detailed explanation of the code is as follows:

  1. mod Function: This is a utility function that calculates the modulo of two long numbers, handling negative values to ensure a positive result.

  2. RNG Class:

    • Instance Variables:
      • a1 and a2: These are arrays of coefficients for two LCGs.
      • m1 and m2: Moduli for the two LCGs.
      • x1 and x2: Arrays for storing the current states of the two LCGs.
    • seed Method: Initializes the LCGs with a specified state value.
    • nextInt Method: Generates the next random integer using the two LCGs and returns it.
    • nextFloat Method: Generates the next random float by dividing the integer result from nextInt by d.
  3. main Method:

    • Creates an RNG instance and seeds it with a value 1234567.
    • Prints five random integers generated using nextInt.
    • Seeds the RNG again with a different value 987654321.
    • Generates 100,000 random floats using nextFloat and counts how often each value from 0 to 4 (inclusive) occurs. The counts are then printed out.

In summary, this code is an implementation of two LCGs to generate random numbers. It demonstrates how to set a seed and generate both random integers and floats using this setup. The mod function helps ensure positive modulo results for non-negative inputs. The main method showcases the random number generation and provides a frequency count for testing the randomness.

Source code in the java programming language

public class App {
    private static long mod(long x, long y) {
        long m = x % y;
        if (m < 0) {
            if (y < 0) {
                return m - y;
            } else {
                return m + y;
            }
        }
        return m;
    }

    public static class RNG {
        // first generator
        private final long[] a1 = {0, 1403580, -810728};
        private static final long m1 = (1L << 32) - 209;
        private long[] x1;
        // second generator
        private final long[] a2 = {527612, 0, -1370589};
        private static final long m2 = (1L << 32) - 22853;
        private long[] x2;
        // other
        private static final long d = m1 + 1;

        public void seed(long state) {
            x1 = new long[]{state, 0, 0};
            x2 = new long[]{state, 0, 0};
        }

        public long nextInt() {
            long x1i = mod(a1[0] * x1[0] + a1[1] * x1[1] + a1[2] * x1[2], m1);
            long x2i = mod(a2[0] * x2[0] + a2[1] * x2[1] + a2[2] * x2[2], m2);
            long z = mod(x1i - x2i, m1);

            // keep the last three values of the first generator
            x1 = new long[]{x1i, x1[0], x1[1]};
            // keep the last three values of the second generator
            x2 = new long[]{x2i, x2[0], x2[1]};

            return z + 1;
        }

        public double nextFloat() {
            return 1.0 * nextInt() / d;
        }
    }

    public static void main(String[] args) {
        RNG rng = new RNG();

        rng.seed(1234567);
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println();

        int[] counts = {0, 0, 0, 0, 0};
        rng.seed(987654321);
        for (int i = 0; i < 100_000; i++) {
            int value = (int) Math.floor(rng.nextFloat() * 5.0);
            counts[value]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.printf("%d: %d%n", i, counts[i]);
        }
    }
}


  

You may also check:How to resolve the algorithm Entropy step by step in the Crystal programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the MiniZinc programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the REXX programming language
You may also check:How to resolve the algorithm Bitmap step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Playing cards step by step in the MUMPS programming language