问题描述
我试图了解使用桃子时随机状态如何传播到线程,但是找不到很多文档。
根据经验,从下面的测试来看,线程最终确实具有不同的种子,但是这些种子可能与主节点的随机种子无关。
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
但是第二次运行两条线并不能重现此答案。
所以我的问题是:
- 线程中的种子与主节点的随机状态之间是否存在关系?
- 我们保证每个线程都能获得不同的种子吗?
- 关于在每个线程设置种子的最佳实践或以其他方式获得可重现结果的任何建议?
谢谢
解决方法
情况并非总是如此,它已在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