之間的隨機數可能重複:
How to generate a random number from within a range - C生成1到N-1
你將如何產生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);
}
提示:查找返回一個隨機數,使用取模運算符的功能 –