u****u 发帖数: 229 | 1 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。
主程序是
{
srand(time(NULL));
for ( i=0;i<10;i++)
{
Thread *pThread = new Thread;
pThread->Begin();//产生一个thread,并使其开始运行
}
}
每个thread的代码是:
{
int data = rand();
cout<
Sleep(1000);
}
可是运行结果是: 每次10个thread都产生相同的数。
比如:一开始产生10个12, 1秒后在一起输出10个502,1秒后再一起输出10个44。
这到底是什么原因?有什么方法可以让每次每个thread都输出不同的数? |
t*s 发帖数: 1504 | 2 change the seed
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
D*******a 发帖数: 3688 | 3 大概是因为所有rand()都access同一个seed了
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
z*****n 发帖数: 7639 | 4 interesting.
I think the problem comes with srand() function, which is
locally done in main(). But this has no effect to threads.
The reason might be each thread is using an independent
code of rand(), if rand() comes with static library.
Try use srand() in each thread.
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
r******e 发帖数: 253 | 5 每个thread里面call srand(time(NULL)+不同值)
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
f****a 发帖数: 4708 | 6 lol you didn't initialized the randome seed.
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
z*****n 发帖数: 7639 | 7 he did in main().
【在 f****a 的大作中提到】 : lol you didn't initialized the randome seed.
|
a******t 发帖数: 100 | 8 then the same seed is shared by all threads. so...
【在 z*****n 的大作中提到】 : he did in main().
|
z*****n 发帖数: 7639 | 9 Actually I am quite confused by this.
If rand() is using monte carlo method, then seeding it
once should be ok because even though it is called by
different thread, it is still done sequentially, not as
you said one seed for all the threads and continues.
Highly likeness is, in threads, rand() is statically linked
and using different buffers to hold sequence.
One can check it by this method:
if the rand() sequence generated in a single main()
without srand() is the same as the poster mentioned
i
【在 a******t 的大作中提到】 : then the same seed is shared by all threads. so...
|
c****s 发帖数: 37 | 10 when a thread is generated, all the data is replicated from main thread.
The context of a rand() is independent of other threads.
【在 z*****n 的大作中提到】 : Actually I am quite confused by this. : If rand() is using monte carlo method, then seeding it : once should be ok because even though it is called by : different thread, it is still done sequentially, not as : you said one seed for all the threads and continues. : Highly likeness is, in threads, rand() is statically linked : and using different buffers to hold sequence. : One can check it by this method: : if the rand() sequence generated in a single main() : without srand() is the same as the poster mentioned
|
|
|
b******p 发帖数: 23 | 11 that's not true.
Processes do that... threads (light-weight processes) don't.
Hard to say what could be wrong without knowing what "new Thread" really does.
【在 c****s 的大作中提到】 : when a thread is generated, all the data is replicated from main thread. : The context of a rand() is independent of other threads.
|
D*******a 发帖数: 3688 | 12 它有可能是所有thread里面,rand()同时执行,所以读了同一个seed
可以试试在thread里面用同步调用
【在 z*****n 的大作中提到】 : Actually I am quite confused by this. : If rand() is using monte carlo method, then seeding it : once should be ok because even though it is called by : different thread, it is still done sequentially, not as : you said one seed for all the threads and continues. : Highly likeness is, in threads, rand() is statically linked : and using different buffers to hold sequence. : One can check it by this method: : if the rand() sequence generated in a single main() : without srand() is the same as the poster mentioned
|
s*****c 发帖数: 753 | 13 srand(int seed)
You only set the seed value at your main program. When your program sprawn
seperate thread later, the seed for each thread is still the same value
returned from time(null) in the main program. There is no surprise each
thread return same sequence.
Just think about it, use srand() to set seed, that seed will be used by rand()
. So the seed must be global variable.
What you need to do is put srand(time(null)) to the constructor of your thread
.
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
r******e 发帖数: 253 | 14 我印象里那个time返回以秒为单位
结果会是所有seed都一样
必须给不同的seed
()
thread
【在 s*****c 的大作中提到】 : srand(int seed) : You only set the seed value at your main program. When your program sprawn : seperate thread later, the seed for each thread is still the same value : returned from time(null) in the main program. There is no surprise each : thread return same sequence. : Just think about it, use srand() to set seed, that seed will be used by rand() : . So the seed must be global variable. : What you need to do is put srand(time(null)) to the constructor of your thread : .
|
z*****n 发帖数: 7639 | 15 you are wrong.
【在 r******e 的大作中提到】 : 我印象里那个time返回以秒为单位 : 结果会是所有seed都一样 : 必须给不同的seed : : () : thread
|
c*r 发帖数: 278 | 16 Depends on what system you are using.
rand() may not be thread-safe.
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
b******p 发帖数: 23 | 17
()
thread
If the seed is a global variable, then how can calling srand multiple times be
of any help?
Following is the typical rand and srand implementation in a standard C lib:
unsigned long next=1;
int rand(void) /* NOT RECOMMENDED (see text) */
{
next = next*1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next=seed;
}
【在 s*****c 的大作中提到】 : srand(int seed) : You only set the seed value at your main program. When your program sprawn : seperate thread later, the seed for each thread is still the same value : returned from time(null) in the main program. There is no surprise each : thread return same sequence. : Just think about it, use srand() to set seed, that seed will be used by rand() : . So the seed must be global variable. : What you need to do is put srand(time(null)) to the constructor of your thread : .
|
r***u 发帖数: 241 | 18 try some OS-supplied random number generator.
For example read from the file /dev/urandom on Linux.
【在 u****u 的大作中提到】 : 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。 : 主程序是 : { : srand(time(NULL)); : for ( i=0;i<10;i++) : { : Thread *pThread = new Thread; : pThread->Begin();//产生一个thread,并使其开始运行 : } : }
|
z*****n 发帖数: 7639 | 19 That is what I tried to explain:
each thread comes with an independent rand() code linked.
be
【在 b******p 的大作中提到】 : : () : thread : If the seed is a global variable, then how can calling srand multiple times be : of any help? : Following is the typical rand and srand implementation in a standard C lib: : unsigned long next=1; : int rand(void) /* NOT RECOMMENDED (see text) */ : { : next = next*1103515245 + 12345;
|
r*******n 发帖数: 51 | 20 1. Add a sleep in your thread if you use a seperate rand instance for each
thread.
or
2. Use only one rand instance for all thread
or
3. Use one rand to generate seed as main rand. Use the seeds for each thread. |