简体   繁体   English

如何在C ++中生成4个不同的随机数

[英]How to generate 4 different random numbers in C++

I am making Bulls and Cows assignment from Bjarne Stroustrup's "Programming Principles and Practice Using C++" book (p. 130, exercise 13) and I want program to generate four different integers in the range 0 to 9 (eg, 1234 but not 1122) 我正在Bjarne Stroustrup的“使用C ++编程原理和实践”一书(第130页,练习13)中进行公牛和牛的分配,我希望程序生成0到9范围内的四个不同整数(例如1234,但不是1122)

I made a vector to store numbers and a function which generates 4 numbers and adds them to vector, but numbers might be the same and I can't return numbers to main function 我制作了一个向量来存储数字,并创建了一个生成4个数字并将其添加到向量的函数,但是数字可能是相同的,因此我无法将数字返回给主函数

#include "../..//..//std_lib_facilities.h"

vector<int> gen4Nums(vector<int> secNum)
{
    random_device rd; // obtain a random number from hardware
    mt19937 eng(rd()); // seed the generator
    uniform_int_distribution<> distr(0, 9); // define the range 

    secNum.clear();
    for (int i = 0; i < 4; i++)
    {
        secNum.push_back(distr(eng));
        cout << secNum[i];
    }
    return secNum;
}

int main()
{
        vector<int> secNum;
        gen4Nums(secNum);   
}

I expect to return 4 different random numbers to the main function 我希望将4个不同的随机数返回给主函数

You can ensure to get different random numbers in the result if you change your code like this: 如果您像这样更改代码,则可以确保获得不同的随机数:

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

using namespace std;

vector<int> gen4Nums()
{
    vector<int> result;
    random_device rd; // obtain a random number from hardware
    mt19937 eng(rd()); // seed the generator
    uniform_int_distribution<> distr(0, 9); // define the range 

    int i = 0;
    while(i < 4) { // loop until you have collected the sufficient number of results
        int randVal = distr(eng);
        if(std::find(std::begin(result),std::end(result),randVal) == std::end(result)) {
        // ^^^^^^^^^^^^ The above part is essential, only add random numbers to the result 
        // which aren't yet contained.
            result.push_back(randVal);
            cout << result[i];
            ++i;
        }
    }
    return result;
}

int main() {
    vector<int> secNum = gen4Nums();   
}

Seems like you're trying to generate 4 unique random integers in the range 0...9. 似乎您正在尝试生成0 ... 9范围内的4个唯一随机整数。

You can do this by generating a vector of integers containing the values 0...9. 您可以通过生成包含值0 ... 9的整数向量来实现此目的。 Then shuffle the vector, as you want it to be a random selection of the integers. 然后将向量随机播放,因为您希望它是整数的随机选择。 Lastly trimming the vector to the desired size, as you only want 4 unique random integers: 最后,将向量修整为所需的大小,因为您只需要4个唯一的随机整数:

#include <vector>
#include <random>
#include <algorithm>
#include <numeric>

void gen4Nums(std::vector<int>& v) {
    //Generate initial vector with values 0...9:
    v.resize(10, 0);
    std::iota(v.begin(), v.end(), 0);

    //Shuffle the vector:
    std::random_device rd;
    std::mt19937 g(rd());
    std::shuffle(v.begin(), v.end(), g);

    //Trim the vector to contain only 4 integers:
    v.resize(4);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM