【发布时间】: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 可能基本上你是对的,但我的编译器没有给出任何错误。