C rand() 函数不生成随机数
C rand() function is not generating random numbers
我是编程新手。我需要一些可以用 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;
}
您需要"seed"随机数生成器。
试试打电话
srand(time(NULL));
一次在程序的顶部。
(有更好的方法,但这应该可以帮助您入门。)
首先,C语言不支持嵌套函数。在您的代码中 main()
的定义中定义 dice_generator()
是非法的。你的编译器可能支持这个,但无论如何这不是 C.
其次,rand()
不生成随机数。 rand()
产生一个看似 "erratic" 但完全确定的整数序列,它从某个初始数字开始并始终遵循相同的路径。您所能做的就是通过使用新种子作为参数调用 srand
,使 rand()
从不同的 "seed" 数字开始其序列。
默认情况下,需要 rand()
才能像您调用 srand(1)
那样为序列播种。
这是代码,经过更正后子函数 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
我是编程新手。我需要一些可以用 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;
}
您需要"seed"随机数生成器。 试试打电话
srand(time(NULL));
一次在程序的顶部。
(有更好的方法,但这应该可以帮助您入门。)
首先,C语言不支持嵌套函数。在您的代码中 main()
的定义中定义 dice_generator()
是非法的。你的编译器可能支持这个,但无论如何这不是 C.
其次,rand()
不生成随机数。 rand()
产生一个看似 "erratic" 但完全确定的整数序列,它从某个初始数字开始并始终遵循相同的路径。您所能做的就是通过使用新种子作为参数调用 srand
,使 rand()
从不同的 "seed" 数字开始其序列。
默认情况下,需要 rand()
才能像您调用 srand(1)
那样为序列播种。
这是代码,经过更正后子函数 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