【发布时间】:2013-01-02 11:02:38
【问题描述】:
可能重复:
How to generate a random number from within a range - C
如何生成一个介于 1 和 N-1 之间的随机数,其中 N 是用户输入的数字?
到目前为止,我的代码是:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number = 0; //number variable
printf("This program will ask you for a number, an integer, and will print out a random number from the range of your number 1, to N-1.");//output that explains the program's purpose and use to the user.
//gets the input
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d", &number); //gets the input and stores it into variable number
return (0);
}
【问题讨论】:
-
提示:寻找一个返回随机数并使用模运算符的函数