Redian新闻
>
为什么多个线程生成的随机数是一样的?
avatar
为什么多个线程生成的随机数是一样的?# CS - 计算机科学
u*u
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都输出不同的数?
avatar
t*s
2
change the seed

【在 u****u 的大作中提到】
: 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。
: 主程序是
: {
: srand(time(NULL));
: for ( i=0;i<10;i++)
: {
: Thread *pThread = new Thread;
: pThread->Begin();//产生一个thread,并使其开始运行
: }
: }

avatar
D*a
3
大概是因为所有rand()都access同一个seed了

【在 u****u 的大作中提到】
: 我有一个程序,主程序产生多个thread,每个thread都要产生一串随机数。
: 主程序是
: {
: srand(time(NULL));
: for ( i=0;i<10;i++)
: {
: Thread *pThread = new Thread;
: pThread->Begin();//产生一个thread,并使其开始运行
: }
: }

avatar
z*n
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,并使其开始运行
: }
: }

avatar
r*e
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,并使其开始运行
: }
: }

avatar
f*a
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,并使其开始运行
: }
: }

avatar
z*n
7
he did in main().

【在 f****a 的大作中提到】
: lol you didn't initialized the randome seed.
avatar
a*t
8
then the same seed is shared by all threads. so...

【在 z*****n 的大作中提到】
: he did in main().
avatar
z*n
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...
avatar
c*s
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

avatar
b*p
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.

avatar
D*a
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

avatar
s*c
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,并使其开始运行
: }
: }

avatar
r*e
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
: .

avatar
z*n
15
you are wrong.

【在 r******e 的大作中提到】
: 我印象里那个time返回以秒为单位
: 结果会是所有seed都一样
: 必须给不同的seed
:
: ()
: thread

avatar
c*r
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,并使其开始运行
: }
: }

avatar
b*p
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
: .

avatar
r*u
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,并使其开始运行
: }
: }

avatar
z*n
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;

avatar
r*n
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.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。