【问题标题】:C rand() function is not generating random numbers [duplicate]C rand()函数不生成随机数
【发布时间】:2015-09-29 19:00:48
【问题描述】:

我是编程新手。我需要一些可以用 C 生成随机数的东西。我找到了“rand()”。但它不会生成随机值。请检查以下简单代码。

以下代码给出

roll the first dice : 6
roll the second dice : 6
roll the third dice : 5

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


int main()
{

  int dice1,dice2,dice3,total1,total2;
  char prediction[10];

int dice_generator()
{
  dice1= (rand()%6)+1;
  printf("roll the first dice: %d \n", dice1);
  dice2= (rand()%6)+1;
  printf("roll the second dice: %d \n", dice2);
  dice3= (rand()%6)+1;
  printf("roll the third dice: %d \n", dice3);

  return 0;
}

  dice_generator();
  total1 = dice1+dice2+dice3;
  printf("final value is = %d\n",total1);
  return 0;
}

【问题讨论】:

  • @user4397355:为什么你的dice_generator()main()里面定义???
  • 您好,欢迎来到 SO!请阅读stackoverflow.com/help/how-to-ask 以更好地了解如何在 Stack Overflow 上提问。
  • 那不是有效的 C 代码。 C 不允许在 block 中定义函数。这是不太有用的 gcc 扩展之一。
  • 谢谢@MagnusKarlsson,我会阅读这些条款以了解如何询问..
  • @olaf 可能基本上你是对的,但我的编译器没有给出任何错误。

标签: c random numbers


【解决方案1】:

您需要“播种”随机数生成器。 尝试调用

srand(time(NULL));

在程序顶部一次。

(有更好的方法,但这应该可以帮助您入门。)

【讨论】:

  • 即使使用srand 而不是严重的RNG,我仍然会从/dev/urandom 播种。
  • @o11c:哦,我也是。但是 OP 说他是“编程新手”,所以找到一个更好的种子将是另一天的一个很好的练习。
  • 谢谢史蒂夫..我把 srand(time(NULL));现在它产生不同的数字。
  • 如果您发现某个答案有帮助,您应该接受最佳答案。它是网站流程的一部分,让未来的观众知道如何解决问题:)
【解决方案2】:

首先,C 语言不支持嵌套函数。在您的代码中定义main() 内定义dice_generator() 是非法的。您的编译器可能支持这一点,但无论如何这不是 C。

其次,rand() 不会生成随机数。 rand() 产生一个看似“不稳定”但完全确定的整数序列,它从某个初始数字开始并始终遵循相同的路径。您所能做的就是通过调用srand 并使用新种子作为参数,使rand() 从不同的“种子”编号开始其序列。

默认情况下,rand() 需要像调用 srand(1) 一样工作,以便为序列播种。

【讨论】:

    【解决方案3】:

    这里是代码,修正后的子函数 dice_generator()

    被正确分离,而不是埋在 main() 中。 (在 C 中,不允许嵌套函数)

    rand() 函数通过 srand() 函数正确初始化。

    未使用的变量( total2 和 prediction[] )被注释掉

    (每行只放置一个变量声明的另一个很好的理由)

    强烈建议在编译时启用所有警告,

    因此您的编译器可以告诉您代码中的问题。

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>
    
    // prototypes
    int dice_generator( void );
    
     // global data
     int dice1;
     int dice2;
     int dice3;
     int total1;
     //int total2;
     //char prediction[10];
    
    int main( void )
    {
    
      srand(time( NULL ));
      dice_generator();
      total1 = dice1+dice2+dice3;
      printf("final value is = %d\n",total1);
      return 0;
    } // end function: main
    
    
    int dice_generator()
    {
      dice1= (rand()%6)+1;
      printf("roll the first dice: %d \n", dice1);
      dice2= (rand()%6)+1;
      printf("roll the second dice: %d \n", dice2);
      dice3= (rand()%6)+1;
      printf("roll the third dice: %d \n", dice3);
    
      return 0;
    } // end function: dice_generator
    

    【讨论】:

    • 对于 gcc,至少使用:'-Wall -Wextra -pedantic std=c99',以便编译器可以告诉您问题(您需要修复)
    • 是的,它会在警告部分显示此类消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多