在C语言中,rand()函数可以用来产生随机数,但是这不是真真意义上的随机数,是一个伪随机数,这篇文章主要介绍了使用c语言生成随机数的示例,需要的朋友可以参考下
这是一个自己用c写的不重复产生随机数的代码,且只有输入q才能退出程序。
#include
#include
#include
#define MAX 100
int main(void)
{
int i, j, flag, num, a[MAX] = { 0 }, max, ch;
srand((unsigned)time(NULL));
printf("Please input max number<1-39>(q to quit): ");
a:while ((scanf("%d", &max)) == 1)
{
if (max >= 40)
{
printf("Please input max number<1-39>(q to quit): ");
while (getchar() != '\n')
continue;
continue;
}
for (i = 0; i
do{num = rand() % 41 + 1;
}while(num==8||num==36);
flag = 1;
for (j = 0; j {
if (num == a[j] || num == 8 || num == 36)
{
flag = 0;
break;
}
}
if (flag)
a[i] = num;
else
--i;
}
while
(getchar() != '\n') continue;
for (i = 0; i
printf("\n");
printf("Please input max number<1-39>(q to quit): ");
}
if (getchar() != 'q')
{
puts("q to quit,please!");
printf("Please input max number<1-39>(q to quit): ");
while (getchar() != '\n')
continue;
goto a;
}
printf("Bye!");
return 0;
}
以上就是使用c语言生成随机数的示例分享的详细内容,更多请关注0133技术站其它相关文章!