Redian新闻
>
ask a C question about random number
avatar
ask a C question about random number# Programming - 葵花宝典
s*e
1
I used
srand((unsigned) time(NULL));
for ( int i=0;i<5;i++)
{
rand();
}
I produce the same number at the same time period such as 5 second or 3 second
.
How can I produce a random number?
avatar
n*s
2
If you invoke this program repeatedly in a short period of time, you are going
to get the same random number sequence because you are calling srand() with
the same seed, namely the integer that is the number of seconds since midnight
1970/01/01. If you must repeatedly invoke the program, you will need to find
a way to hash up the seed value. You could try putting the program to sleep
for a second or two before calling srand().

second

【在 s*******e 的大作中提到】
: I used
: srand((unsigned) time(NULL));
: for ( int i=0;i<5;i++)
: {
: rand();
: }
: I produce the same number at the same time period such as 5 second or 3 second
: .
: How can I produce a random number?

avatar
L*r
3
But I test it, it generates different numbers?

second

【在 s*******e 的大作中提到】
: I used
: srand((unsigned) time(NULL));
: for ( int i=0;i<5;i++)
: {
: rand();
: }
: I produce the same number at the same time period such as 5 second or 3 second
: .
: How can I produce a random number?

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