简体   繁体   English

在 C 的 rand() 中,我每次都得到相同的值

[英]I am getting same value for every time in rand() in C

In this program, I need to take 2 random numbers and one of them must be equal to 3. To make them, the randomSet function works with a while loop, but the problem is in the main function: the loop gives me same 2 numbers every time.在这个程序中,我需要取 2 个随机数,其中一个必须等于 3。要制作它们, randomSet function 与 while 循环一起工作,但问题出在主 function 中:循环给了我相同的 2 个数字每次。 How do I get different numbers on each iteration?如何在每次迭代中获得不同的数字?

int randomSet(int temp[])
{
  while (temp[0] != 3 && temp[1] != 3)
         {
            temp[0] =rand()%4;
            temp[1] =rand()%4;
         }
}

the main function is主要的 function 是

int main()
{
    srand(time(0));
    int temp[2];
    int x =0,y =0;
    for (int i = 0; i < 5; i++) {
        randomSet(temp);
    }
    return 0;
}

You are not initializing temp before passing it to randomSet() , so its contents are indeterminate , causing while (temp[0] != 3 && temp[1] != 3) to have undefined behavior .您没有在将temp传递给randomSet()之前对其进行初始化,因此它的内容是不确定的,导致while (temp[0] != 3 && temp[1] != 3)具有未定义的行为

I suggest you change randomSet() to use a do..while loop instead so that you populate temp 1 time before you then start analyzing its content, eg:我建议您将randomSet()更改为使用do..while循环,以便在开始分析其内容之前填充temp 1 次,例如:

int randomSet(int temp[])
{
    do
    {
        temp[0] = rand() % 4;
        temp[1] = rand() % 4;
    }
    while (temp[0] != 3 && temp[1] != 3);
}

You can easily instrument the code and see what goes on.您可以轻松地检测代码并查看发生了什么。 As already pointed out, the key necessary change is that you must determine two random values on entry to the function before checking whether either (or both) of them is 3. That is easily achieved with a do { … } while (…);正如已经指出的那样,关键的必要更改是您必须在进入 function 时确定两个随机值,然后再检查它们中的一个(或两个)是否为 3。这很容易通过do { … } while (…);实现。 loop.环形。

Despite suggestions to the contrary, the loop condition in the question is OK, though it needs to be converted from a while loop.尽管有相反的建议,但问题中的循环条件是可以的,尽管它需要从while循环转换。 The loop continues while neither element is 3;当两个元素都不为 3 时,循环继续; as soon as one or the other is 3 (or both are 3, but it only checks the first element, temp[0] ), the loop stops.一旦一个或另一个是 3(或两者都是 3,但它只检查第一个元素temp[0] ),循环就会停止。

For example (file rand19.c ):例如(文件rand19.c ):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static void randomSet(int temp[])
{
    do
    {
        temp[0] = rand() % 4;
        temp[1] = rand() % 4;
        printf("--  %d  %d\n", temp[0], temp[1]);
    } while (temp[0] != 3 && temp[1] != 3);
}

int main(void)
{
    srand(time(0));
    int temp[2];
    for (int i = 0; i < 5; i++)
    {
        randomSet(temp);
        printf("%d:  %d  %d\n", i, temp[0], temp[1]);
    }
    return 0;
}

Example runs:示例运行:

$ rand19
--  2  0
--  1  3
0:  1  3
--  0  3
1:  0  3
--  0  3
2:  0  3
--  2  2
--  0  1
--  3  0
3:  3  0
--  2  0
--  2  0
--  1  0
--  1  0
--  2  3
4:  2  3
$ sleep 60
$ rand19
--  3  3
0:  3  3
--  1  3
1:  1  3
--  1  0
--  3  1
2:  3  1
--  3  2
3:  3  2
--  0  2
--  1  1
--  1  2
--  0  1
--  1  2
--  2  1
--  3  1
4:  3  1
$
int randomSet(int temp[])
{
  while (1)
         {
            x = rand()%4;
            y = rand()%4;
            if (x < 3 && y < 3) 
            {
               temp[0] = x;
               temp[1] = y;
            }
            else
               break;
         }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM