使用1到75之间的随机数创建一个不同的矩阵

时间:2017-03-25 10:21:34

标签: c++11

我尝试为宾果游戏创建一个从1到75的随机数矩阵。

但是,我无法生成不同的数字。

第一行不应该是1到15,第二行应该是16到30,第三行应该是31到45,第四行应该是46到60,第五行是61到75.

void creatingbingo()
{ int ar[5][5],values[5],i,j,k,n;


//creating bingo ticket
//initializing the values of ticket
for(i=0;i<=4;i++)
{
    values[i]=0;
}
for(i=0;i<=4;i++)
{
    for(j=0;j<=4;j++)
    {
        loop:k=0;
        do
        {
            uniform_int_distribution<int> rows (((15*(i+1)-15+1)),(15*(i+1)));
            n=rows(randgenerate);
            if(values[k]==n)
            {
                k=0;
                goto loop;
            }
            else
            {
                values[k++]=n;
            }
        }while(k<5);
            ar[i][j]=values[j];//filling the ticket
    }
}
//displaying the ticket
for(i=0;i<5;i++)
{
    for(j=0;j<5;j++)
    {
        cout<<ar[i][j]<<'\t';
    }
    cout<<endl;
}
}

 An example of output which I got is:
 11      14      8       5       10
 16      26      24      25      19
 41      45      38      43      31
 55      52      60      53      60
 62      70      71      68      75

1 个答案:

答案 0 :(得分:0)

有一种更简单的方法:从1到15创建一个包含15个元素的数组b。对于每一行a[i],随机播放b,然后复制前5 brow的元素,并将15*i添加到a[i]的每个元素。

工作代码示例:

#include <iostream>
#include <random>
#include <boost/random/random_device.hpp>
#include <vector>
#include <numeric>
#include <algorithm>

std::mt19937 rng{boost::random_device{}()};

std::vector<std::vector<int>> creatingbingo()
{
    std::vector<std::vector<int>> a(5, std::vector<int>(5));
    std::vector<int> b(15);
    std::iota(begin(b), end(b), 1); //b = 1 2 3 ... 14 15
    for (size_t i = 0; i < a.size(); ++i)
    {
        std::shuffle(begin(b), end(b), rng);
        std::copy(begin(b), begin(b) + a[i].size(), begin(a[i]));
        for (int& n : a[i]) n += 15*i;
    }
    return a;
}

int main()
{
    for (auto& row : creatingbingo())
    {
        for (int n : row)
            std::cout << n << "\t";
        std::cout << "\n";
    }
}

&#34;更简单&#34;版本:

#include <iostream>
#include <algorithm> //std::shuffle
#include <random>    //std::mt19937
#include <ctime>     //to seed rng

struct BingoTicket { //create a wrapper struct to return 5x5 matrix
    int grid[5][5];
};

BingoTicket createBingoTicket(std::mt19937& rng)
{
    BingoTicket ticket;

    // Create an array of 15 elements with values from 1 to 15
    int b[15];
    for (int i = 0; i < 15; i++)
        b[i] = i+1;

    for (int i = 0; i < 5; ++i) //iterate each row of `ticket.grid`
    {
        // Shuffle b using a given random generator `rng`
        std::shuffle(b, b+15, rng);
        // Copy first 5 elements in `b` to `ticket.grid[i]`,
        //and add 0/15/30/45/60 so that row 1 will have values
        //from 1-15, row 2 from 16-30, etc.
        for (int j = 0; j < 5; j++)
            ticket.grid[i][j] = b[j] + 15*i;
    }

    return ticket;
}

int main()
{
    // Seed rng
    std::mt19937 rng;
    rng.seed(time(0));

    BingoTicket ticket = createBingoTicket(rng);
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
            std::cout << ticket.grid[i][j] << "\t";
        std::cout << "\n";
    }
}

(您可以使用std::random_device种子rng,或使用std::chrono代替ctime