可能重复:
如何从一个范围内生成一个随机数 - 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);
}