我想创建一个程序,提示用户生成一定数量的随机数(1到20之间)。然后,程序生成介于 (1 和 40) 之间的数字数量。生成的数字都应该是唯一的。
我不知道如何让他们产生一个唯一的数字。
如果您想
在每次运行程序时生成唯一的随机数,请尝试此操作;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main(void)
{
int n, num;
printf("Enter the amount of numbers to generate");
scanf("%d", &n);
bool temp[40];
srand((unsigned)(time(NULL)));
for (int i=0; i<n+1; i++){
temp[i] = false;
}
for (int i=0; i<n;)
{
num = 1 + rand()%40;
if(temp[num] == false)
{
printf("%dn", num);
temp[num] = true;
i++;
}
}
return 0;
}