#include stdio.h #include stdlib.h #include time.h int main() {int iRandomNum = 0;
编程学习网为您整理以下代码实例,主要实现:使用srand()函数生成随机数,希望可以帮到各位朋友。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int iRandomNum = 0;
srand(123);
iRandomNum = (rand() % 4) + 1;
printf("\nFortune cookie - Chapter 3\n");
switch (iRandomNum) {
case 1:
printf("\n a.\n");
break;
case 2:
printf("\n b.\n");
break;
case 3:
printf("\n c.\n");
break;
case 4:
printf("\n d.\n");
break;
}
printf("%d\n ", (rand() % 49) + 1);
printf("%d\n ", (rand() % 49) + 1);
printf("%d\n ", (rand() % 49) + 1);
printf("%d\n ", (rand() % 49) + 1);
printf("%d\n ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
}
沃梦达教程
本文标题为:使用srand()函数生成随机数


猜你喜欢
- “纯虚函数调用"在哪里?崩溃从何而来? 2022-10-18
- 使用最流行的转义序列 1970-01-01
- C++浮点常数 1970-01-01
- C语言求模 1970-01-01
- C语言可使用的所有转义序列 1970-01-01
- 使用整数值初始化char类型的变量 1970-01-01
- C++指向数组的指针 1970-01-01
- 运算符优先级 1970-01-01
- 使用来自float.h和limits的数据,找到该系统的一些 1970-01-01
- 打印扩展的ASCII字符 1970-01-01