C中的随机数

发布于 2024-08-25 09:20:39 字数 477 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

記憶穿過時間隧道 2024-09-01 09:20:39

在循环之外调用 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 to time(NULL) always returns the same value. You are resetting to the same random sequence with every iteration. As a general rule, only call srand() once in your program.

枕花眠 2024-09-01 09:20:39

不要每次循环都调用 srand() - 只需提前调用一次即可。

Don't call srand() every time through the loop - just do it once beforehand.

等风也等你 2024-09-01 09:20:39

常见问题解答 13.1513.20 将会引起人们的兴趣。我很想为此类问题创建一个新标签。

FAQs 13.15 to 13.20 will be of interest. And I am tempted to create a new tag for such questions.

很酷不放纵 2024-09-01 09:20:39

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 use srand to tell it where to start in that list, with each call to rand() 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.

阿楠 2024-09-01 09:20:39
srand(time(NULL)); 

for(i = 0; i < n; i++){         
        printf("%d ", time(NULL)); 
        for(j = 0; j < (n-1); j++){ 
            a[i,j] = rand(); 
        } 
    } 

在循环外调用 srand 一次。

srand(time(NULL)); 

for(i = 0; i < n; i++){         
        printf("%d ", time(NULL)); 
        for(j = 0; j < (n-1); j++){ 
            a[i,j] = rand(); 
        } 
    } 

Call srand once outside the loop.

流心雨 2024-09-01 09:20:39

在进入循环之前,您需要调用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.

一张白纸 2024-09-01 09:20:39
srand(time(NULL));
for(i = 0; i < n; i++){
    for(j = 0; j < (n-1); j++){
        a[i,j] = rand();
    }
}

不管。数量是一样的...

srand(time(NULL));
for(i = 0; i < n; i++){
    for(j = 0; j < (n-1); j++){
        a[i,j] = rand();
    }
}

No matter. The number are the same...

千紇 2024-09-01 09:20:39
int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);
if( n < 1 ){
    printf("Size should be > 0\n\n");
    return NULL;
}
a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
    a[i] = (int*)calloc(n-1, sizeof(int));

这是我的数组...

int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);
if( n < 1 ){
    printf("Size should be > 0\n\n");
    return NULL;
}
a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
    a[i] = (int*)calloc(n-1, sizeof(int));

Here is my array...

我是有多爱你 2024-09-01 09:20:39

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, writing a[i,j] is identical to a[j]. What you received in the print was the value of the pointer to the j-th vector in your matrix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文