尽管使用 "srand" (gcc) 结果相同
Same result although using "srand" (gcc)
在下一个代码中,尽管使用“srand”并遵循下一个 link rand() and srand() in C/C++:
的说明,但我每次都会得到相同的结果
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
int i, x;
srand(5);
for(i=1;i<=10;++i)
{
x=rand()%5;
cout << x << endl;
}
return 0;
}
您得到相同结果的原因是您始终将相同的值 (5) 传递给 srand
。
如链接页面所述,srand(time(0))
是让种子发生变化的常用方法。
要在快速循环中使用它,您必须对其进行不同的编码。
我为您举了一个例子,它产生了与您发布的不同的随机数...
https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/
...和来自这里的@Martin York'示例...
Recommended way to initialize srand?
rnd.c
// C program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Driver program
int main(void)
{
struct timeval time;
// gettimeofday(&time,NULL);
// microsecond has 1 000 000
// Assuming you did not need quite that accuracy
// Also do not assume the system clock has that accuracy.
// The trouble here is that the seed will repeat every
// 24 days or so.
// If you use 100 (rather than 1000) the seed repeats every 248 days.
// Do not make the MISTAKE of using just the tv_usec
// This will mean your seed repeats every second.
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
for(int i = 0; i<4; i++)
printf("%d\n", rand());
return 0;
}
和运行编译后:for i in {1 2 3 4 5}; do ./rnd; done
印象...
$ gcc rnd.c -o rnd
$ for i in {1..5}; do ./rnd; done
1158880197
501076563
213577277
1251321038
1148335733
2072905546
1342241427
1561109312
380708773
620742083
852878351
1021323199
1918572767
2022993565
1242913309
956395645
2100425479
1520573396
1834768200
693311397
如果您在 rnd.c 中评论 // for(int i = 0; i<4; i++)
它可以用于随机日期' ...
$ for i in {1..5};do date --date='@'$(./rnd); done
Mi 19. Dez 03:19:20 CET 1979
Fr 22. Aug 11:56:59 CEST 1980
Di 13. Jul 17:47:54 CEST 1982
Fr 3. Mai 19:59:37 CEST 2002
Fr 27. Apr 09:33:41 CET 1979
在下一个代码中,尽管使用“srand”并遵循下一个 link rand() and srand() in C/C++:
的说明,但我每次都会得到相同的结果#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
int i, x;
srand(5);
for(i=1;i<=10;++i)
{
x=rand()%5;
cout << x << endl;
}
return 0;
}
您得到相同结果的原因是您始终将相同的值 (5) 传递给 srand
。
如链接页面所述,srand(time(0))
是让种子发生变化的常用方法。
要在快速循环中使用它,您必须对其进行不同的编码。
我为您举了一个例子,它产生了与您发布的不同的随机数...
https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/
...和来自这里的@Martin York'示例...
Recommended way to initialize srand?
rnd.c
// C program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Driver program
int main(void)
{
struct timeval time;
// gettimeofday(&time,NULL);
// microsecond has 1 000 000
// Assuming you did not need quite that accuracy
// Also do not assume the system clock has that accuracy.
// The trouble here is that the seed will repeat every
// 24 days or so.
// If you use 100 (rather than 1000) the seed repeats every 248 days.
// Do not make the MISTAKE of using just the tv_usec
// This will mean your seed repeats every second.
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
for(int i = 0; i<4; i++)
printf("%d\n", rand());
return 0;
}
和运行编译后:for i in {1 2 3 4 5}; do ./rnd; done
印象...
$ gcc rnd.c -o rnd
$ for i in {1..5}; do ./rnd; done
1158880197
501076563
213577277
1251321038
1148335733
2072905546
1342241427
1561109312
380708773
620742083
852878351
1021323199
1918572767
2022993565
1242913309
956395645
2100425479
1520573396
1834768200
693311397
如果您在 rnd.c 中评论 // for(int i = 0; i<4; i++)
它可以用于随机日期' ...
$ for i in {1..5};do date --date='@'$(./rnd); done
Mi 19. Dez 03:19:20 CET 1979
Fr 22. Aug 11:56:59 CEST 1980
Di 13. Jul 17:47:54 CEST 1982
Fr 3. Mai 19:59:37 CEST 2002
Fr 27. Apr 09:33:41 CET 1979