使用peach时,随机状态如何传播到不同的线程



我试图了解使用peach时随机状态是如何传播到线程的,但找不到太多关于它的文档。

根据经验,从下面的测试来看,线程似乎最终会有不同的种子,但这些种子可能与主线程的随机种子无关。

q) S 123 
q) {[x] show 20?10} peach til 10
5 1 5 7 4 4 2 2 4 9 0 7 6 4 5 2 1 7 1 9
0 3 9 6 4 3 6 0 9 1 6 7 4 4 0 0 7 0 8 4
5 7 9 4 6 8 8 6 4 6 4 7 7 6 8 9 8 7 4 2
2 8 4 2 3 2 0 3 6 3 1 2 7 8 8 3 9 2 7 6
1 0 3 9 5 2 7 5 5 3 1 2 6 1 8 9 5 2 5 5
2 6 6 7 2 0 6 9 1 4 7 9 9 8 2 7 1 4 4 3
3 2 2 0 0 9 2 6 3 1 6 1 6 5 3 4 6 0 8 9
2 3 0 3 4 3 9 0 8 1 5 7 1 1 3 3 0 5 3 4
0 9 5 2 1 2 0 2 6 4 5 7 9 7 2 6 9 1 9 8
5 7 8 0 5 6 0 2 0 0 0 5 6 4 4 8 3 9 9 2

但是第二次运行两条线路并不能重现这个答案。

所以我的问题是:

  1. 线程处的种子与主进程的随机状态之间是否存在关系
  2. 我们是否保证在每根线上都能得到不同的种子
  3. 关于在每一个线程上设置种子的最佳实践或获得可重复结果的其他方法,有什么建议吗

感谢

情况并非总是如此,它在v3.1 中得到了修复

q).z.K
3f
q)count distinct {10?10}peach til 1000
931
q).z.K
4f
q)count distinct {10?10}peach til 1000
1000

从发行说明

2013.08.19
FIX
the random number generator was not thread-safe. Now the behaviour is as follows
rng is thread local.
S 1234 sets the seed for the rng for the main thread only.
the rng in a slave thread is assigned a seed based on the slave thread number.
in multithreaded input mode, the seed is based on socket descriptor.
instances started on ports 20000 thru 20099 (slave procs, used with e.g. q -s -4) have the main thread's default seed based on the port number.
q)s
-2i
q)
q){value"\q -p ",string x}each 6000+til 2;
q).z.pd:`u#hopen each 6000+til 2
q)count distinct{10?20}peach til 1000 // uniqueness not maintained across slave processes
502
q)count distinct({10?20}each til 1000),{10?20}peach til 1000 // uniqueness not maintained across master+slaves
1002
q)
q){value"\q -p ",string x}each 20000+til 2; // ports 20000 & above as per notes
q).z.pd:`u#hopen each 20000+til 2
q)count distinct{10?20}peach til 1000 // unique across slaves
1000
q)count distinct({10?20}each til 1000),{10?20}peach til 1000 // unique across master+slaves
2000

最新更新