用c语言编写一个随机点名器程序
时间: 2024-06-09 14:09:49 浏览: 15
以下是一个简单的随机点名器程序,使用C语言编写:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
// 定义学生名单
char *students[] = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"};
int num_students = sizeof(students) / sizeof(students[0]);
// 初始化随机数生成器
srand(time(NULL));
// 生成随机数
int random_index = rand() % num_students;
// 输出结果
printf("今天被点名的同学是:%s\n", students[random_index]);
return 0;
}
```
解释:
1. 定义了一个字符串数组 `students`,存储了学生名单。
2. 计算学生名单的数量。
3. 使用 `srand` 函数初始化随机数生成器,传入 `time(NULL)` 作为种子,使得每次运行程序时生成的随机数都不相同。
4. 使用 `rand` 函数生成一个在 0 到学生名单数量之间的随机数。
5. 根据随机数从学生名单中选出一名学生。
6. 使用 `printf` 函数输出结果。
相关问题
用C语言设计一个随机点名器
下面是一个使用C语言设计的简单随机点名器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// 学生名单
char* students[] = {
"张三",
"李四",
"王五",
"赵六",
"钱七"
};
// 获取学生名单长度
int numStudents = sizeof(students) / sizeof(students[0]);
// 设置随机种子
srand(time(NULL));
// 随机生成一个索引
int randomIndex = rand() % numStudents;
// 输出随机点到的学生名字
printf("今天被点名的同学是:%s\n", students[randomIndex]);
return 0;
}
```
这个程序首先定义了一个学生名单数组,然后通过生成一个随机索引来随机点名,并输出被点到的学生名字。每次运行该程序,都会随机选择一个学生名字进行输出。
请注意,在每次运行程序时,由于使用了时间作为随机种子,所以每次运行的结果都可能不同。
用C语言编写一个随机密码器
好的,我可以回答这个问题。以下是一个用C语言编写的随机密码器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PASSWORD_LENGTH 8
int main() {
char password[PASSWORD_LENGTH + 1];
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const int charset_size = sizeof(charset) - 1;
int i;
srand(time(NULL));
for (i = 0; i < PASSWORD_LENGTH; i++) {
password[i] = charset[rand() % charset_size];
}
password[PASSWORD_LENGTH] = '\0';
printf("Random password: %s\n", password);
return 0;
}
```
这个程序会生成一个由大小写字母和数字组成的8位随机密码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)