c++随机生成100个点的代码
时间: 2023-08-24 11:16:55 浏览: 51
以下是使用 C++ 随机生成 100 个点的代码示例:
```c++
#include <iostream>
#include <cstdlib>
using namespace std;
struct Point {
int x;
int y;
};
int main() {
Point points[100];
// 初始化随机数种子
srand(time(NULL));
// 随机生成 100 个点
for (int i = 0; i < 100; i++) {
points[i].x = rand() % 100; // x 坐标在 0~99 之间随机生成
points[i].y = rand() % 100; // y 坐标在 0~99 之间随机生成
}
// 输出所有点的坐标
for (int i = 0; i < 100; i++) {
cout << "Point " << i + 1 << ": (" << points[i].x << ", " << points[i].y << ")" << endl;
}
return 0;
}
```
该代码生成了一个包含 100 个随机点的数组,并输出了所有点的坐标。
相关问题
C++随机生成一个迷宫的代码,
以下是一个简单的C++代码示例,用于随机生成一个迷宫:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ROW = 10; // 迷宫的行数
const int COL = 10; // 迷宫的列数
int main()
{
srand(time(NULL)); // 初始化随机数种子
int maze[ROW][COL]; // 迷宫矩阵,0表示通道,1表示墙
int startRow, startCol; // 起点的行列坐标
int endRow, endCol; // 终点的行列坐标
// 初始化迷宫
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (i == 0 || j == 0 || i == ROW - 1 || j == COL - 1) {
maze[i][j] = 1; // 边界上的格子为墙
} else {
maze[i][j] = rand() % 2; // 随机生成通道或墙
}
}
}
// 随机生成起点和终点
startRow = rand() % (ROW - 2) + 1;
startCol = rand() % (COL - 2) + 1;
do {
endRow = rand() % (ROW - 2) + 1;
endCol = rand() % (COL - 2) + 1;
} while (endRow == startRow && endCol == startCol);
// 在起点和终点处打开通道
maze[startRow][startCol] = 0;
maze[endRow][endCol] = 0;
// 输出迷宫
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (maze[i][j] == 0) {
cout << " "; // 通道用两个空格表示
} else {
cout << "■"; // 墙用实心正方形表示
}
}
cout << endl;
}
return 0;
}
```
该程序中,首先使用`srand(time(NULL))`初始化随机数种子,然后使用双重循环生成迷宫矩阵,其中边界上的格子为墙,内部格子随机生成通道或墙。接着随机生成起点和终点,最后在起点和终点处打开通道。最后输出迷宫,通道用两个空格表示,墙用实心正方形表示。
请帮我随机生成一个C++代码
当然可以!以下是一个随机生成长度为10的整数数组并打印的C++代码示例:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
// 设置随机种子
std::srand(std::time(0));
const int length = 10;
int array[length];
// 随机生成数组元素
for (int i = 0; i < length; ++i) {
array[i] = std::rand() % 100; // 生成0-99之间的随机数
}
// 打印数组
for (int i = 0; i < length; ++i) {
std::cout << array[i] << " ";
}
return 0;
}
```
这段代码会生成一个长度为10的整数数组,并打印出数组中的元素。每次运行结果都会不同,因为使用了随机数生成函数。希望对你有帮助!如果有其他问题,请随时提问。