mardi 18 décembre 2018

Use PractRand with Java Jar

I am trying to use PractRand to test the output of my RandomNumberGenerator. My understanding is that PractRand can read bytes from the StdIn.

public class App {

public static void main(String[] args) {

    RandomNumberGenerator rng = new RandomNumberGenerator();
    rng.initiateRandomGenerator();

        for (int i = 0; i < 1000000; i++) {
            List<Integer> output = rng.draw(0, 9, 1);
            byte num = output.get(0).byteValue();
            System.out.write(num);
        }

  }
}

And then java -jar target/rng-0.0.1-SNAPSHOT-jar-with-dependencies.jar | ~/Downloads/PractRand/RNG_test stdin64

The above fails however with Segmetation fault given from PractRand. Apparently I am doing something incorrectly but I don't know what. A cpp program that could work is

#include <cstdio>
#include <cstddef>
#include <cstdint>

#include <random>

int main()
{
freopen(NULL, "wb", stdout);  // Only necessary on Windows, but harmless. 

std::mt19937_64 rng(42);
constexpr size_t BUFFER_SIZE = 1024 * 1024 / sizeof(uint64_t);
static uint64_t buffer[BUFFER_SIZE];

while (1) {
    for (size_t i = 0; i < BUFFER_SIZE; ++i)
        buffer[i] = rng();
    fwrite((void*) buffer, sizeof(buffer[0]), BUFFER_SIZE, stdout);
}
}

So I guess the fwrite in cpp is different to what I am doing in Java. How though?




Aucun commentaire:

Enregistrer un commentaire