我在 Unix 系统上使用 srandom() 和 random() 在 c 中生成随机数。我想要多个RNG。每个给定相同的种子,都应该输出相同的序列。我还想保存和恢复每个的状态。这是一个伪代码示例:
R1 = new_rng(5); //5 is the seed
R2 = new rng(5); //5 is the seed here, too.
a = R1.random();
b = R1.random();
d = R2.random(); //a == d
s1 = R2.get_state(); //save the state of R2
e = R2.random(); //b == e
R2.set_state(s1); //restore the state of R2
f = R2.random(); //b == f
我该怎么做呢?有时 RNG 会分叉到不同的线程中,我也需要在创建新线程时复制 RNG 的状态。