我正在尝试使用PractRand测试我的RandomNumberGenerator的输出。我的理解是PractRand可以从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);
}
}
}
然后是java -jar target/rng-0.0.1-SNAPSHOT-jar-with-dependencies.jar | ~/Downloads/PractRand/RNG_test stdin64
但是,使用PractRand给出的Segmetation fault
,上述操作将失败。显然我做错了什么,但我不知道该怎么办。可能有效的cpp程序是
#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);
}
}
所以我想cpp中的fwrite
与我在Java中所做的不同。怎么会