以下是使用 C++ 随机排列 0、1、2 三个元素的示例代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>

int main() {
    // 创建包含 0,1,2 的数组
    std::vector<int> arr = {0, 1, 2};

    // 使用随机数引擎进行随机排列
    std::random_device rd;
    std::mt19937 g(rd());
    std::shuffle(arr.begin(), arr.end(), g);

    // 输出随机排列后的结果
    for (const auto& num : arr) {
        std::cout << num << ' '; 
    }
    std::cout << std::endl;

    return 0;
}

运行代码后,输出的结果将是一个随机排列的 0、1、2。

C++ 随机排列 0、1、2 三个元素的数组

原文地址: https://www.cveoy.top/t/topic/lMGr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录