C中的随机数
for(i = 0; i < n; i++){
srand(time(NULL));
printf("%d ", time(NULL));
for(j = 0; j < (n-1); j++){
a[i][j] = rand();
}
}
我尝试生成随机数,但它们是相同的...我尝试 srand(i * time(NULL)) 。不管.. 我应该怎么办?
数组声明:
int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);
a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
a[i] = (int*)calloc(n-1, sizeof(int));
for(i = 0; i < n; i++){
srand(time(NULL));
printf("%d ", time(NULL));
for(j = 0; j < (n-1); j++){
a[i][j] = rand();
}
}
I try to generate random numbers, but they are the same... I try srand(i * time(NULL))
. No matter..
What should i do?
Array declaration:
int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);
a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
a[i] = (int*)calloc(n-1, sizeof(int));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在循环之外调用 srand() 。你每次迭代都会重新播种它。
srand()
为随机数生成器提供种子,以便根据输入获得不同的随机数序列。您的循环运行得非常快,因此对time(NULL)
的调用始终返回相同的值。每次迭代都会重置为相同的随机序列。作为一般规则,仅在程序中调用 srand() 一次。Call
srand()
outside of the loop. You are reseeding it every iteration.srand()
seeds the random number generator so you get a different sequence of random numbers depending on the input. Your loop runs very fast, so the call totime(NULL)
always returns the same value. You are resetting to the same random sequence with every iteration. As a general rule, only callsrand()
once in your program.不要每次循环都调用
srand()
- 只需提前调用一次即可。Don't call
srand()
every time through the loop - just do it once beforehand.常见问题解答 13.15 至 13.20 将会引起人们的兴趣。我很想为此类问题创建一个新标签。
FAQs 13.15 to 13.20 will be of interest. And I am tempted to create a new tag for such questions.
srand
是一个为随机数生成器“播种”的函数。如果您不知道,计算机中的随机数并不是真正随机的。实际上,计算机只有一个看似随机的数字列表,每次调用rand()
时,您都可以使用 srand 告诉它从该列表中的何处开始。 code> 返回列表中的下一项。您编写 srand(time(NULL)) 的原因是为了让随机数在某个点开始,而每次运行程序时该随机数都不会相同(除非程序开始于同时)。
所以你在这里所做的就是反复告诉程序在同一点重新启动随机数列表(因为每次循环的时间都是相同的)。将对 srand 的调用移到循环之外,您将获得正确的结果。
srand
is a function that "seeds" the random number generator. In case you don't know, random numbers in computers aren't really random. In effect, the computer just has a list of numbers that seem random in it, and you usesrand
to tell it where to start in that list, with each call torand()
returning the next item in the list.The reason you write
srand(time(NULL))
is to get the random numbers to start at some point that isn't going to be the same every time you run the program (unless the programs start at the same time).So what you are doing here is repeatedly telling the program to restart the random number list at the same point (because the time is the same each time you go through the loop). Move the call to
srand
outside the loop and you will get the correct results.在循环外调用 srand 一次。
Call srand once outside the loop.
在进入循环之前,您需要调用
srand()
。srand()
使用给定的种子初始化 radnom 数生成器,并为此种子生成唯一的随机数序列。您的循环执行速度非常快,因此每次调用
time(NULL)
都会产生相同的时间(以秒为单位) - 因此您在每次循环迭代时使用相同的种子初始化随机数生成器。You need to call
srand()
before entering the loop.srand()
initializes the radnom number generator with the given seed and generates unique sequence of random numbers for this seed.Your loop executes very fast so every call to
time(NULL)
yields the same time (measured in seconds) - hence you initialize random number generator with the same seed on every loop iteration.不管。数量是一样的...
No matter. The number are the same...
这是我的数组...
Here is my array...
Sergey,您没有收到
a[i,j]
版本的错误消息,只是因为这是一个完全有效的表达式。逗号运算符从左到右计算子表达式并返回最后一个表达式的值。因此,编写a[i,j]
与a[j]
相同。您在打印中收到的是指向矩阵中第 j 个向量的指针的值。Sergey, you did not get an error message with the
a[i,j]
version simply because this is a perfectly valid expression. The comma operator evaluates the sub-expressions from left to right and returns the value of the last expression. Thus, writinga[i,j]
is identical toa[j]
. What you received in the print was the value of the pointer to the j-th vector in your matrix.