手机号码随机生成 c++
时间: 2023-10-18 17:18:01 浏览: 52
以下是一个简单的示例代码,用于在 C++ 中生成随机手机号码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
cout << "随机生成的手机号码为:" << endl;
for (int i = 0; i < 11; i++) {
if (i == 0) {
cout << "1";
}
else {
cout << rand() % 10;
}
if (i == 2 || i == 6) {
cout << "-";
}
}
cout << endl;
return 0;
}
```
该代码中,我们使用 `srand()` 函数和 `time(NULL)` 函数来生成一个随机种子,然后使用 `rand()` 函数生成随机数字。最后,我们根据手机号码的格式输出随机生成的手机号码。
相关问题
c++随机生成菜谱
你可以使用C++的随机数生成器和数组来实现随机生成菜谱。以下是一个简单的示例代码:
```c++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL)); // 设置随机数种子
string dishes[] = {"红烧肉", "鱼香肉丝", "宫保鸡丁", "清蒸鲈鱼", "糖醋里脊", "麻婆豆腐"}; // 菜品数组
int num_dishes = sizeof(dishes) / sizeof(dishes[0]); // 计算菜品个数
int num_selected = rand() % 3 + 2; // 随机选择2-4个菜品
cout << "今天的菜单是:" << endl;
for (int i = 0; i < num_selected; i++) {
int index = rand() % num_dishes; // 随机选择一个菜品
cout << dishes[index] << endl;
}
return 0;
}
```
这个程序会随机选择2到4个菜品,并输出这些菜品的名字。你可以根据需要修改菜品数组和随机数范围来实现不同的生成菜谱效果。
随机生成字符串c++
可以使用C++的随机数生成函数和字符数组来生成随机字符串,示例代码如下:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
const int len = 10; // 字符串长度
char str[len+1]; // 字符串数组
srand(time(NULL)); // 初始化随机数种子
for (int i = 0; i < len; i++) {
str[i] = 'a' + rand() % 26; // 生成随机小写字母
}
str[len] = '\0'; // 字符串结尾
cout << str << endl; // 输出随机字符串
return 0;
}
```