adding letters randomly into an array

How do I add letters from A-Y in random places in a 2d array that has 50 columns and 10 rows?

void playersgrid(char two[GRIDY][GRIDX]) //, char three[PLAYERCNT])
{
int a,b;
for(int i=0;i<GRIDY;i++)
{
for(int j=0; j<GRIDX; j++)
{
a=rand()%GRIDY;
b=rand()%GRIDX;
if(ALIVE)
two[a][b]=0;

//two[i][j]=0;
}
}
/* for(int i= 0 ;i < PLAYERCNT;i++)
{
a = rand()%GRIDY;
b = rand()%GRIDX;
two[a][b]= three[i];
if(two[a][b]==0)
cout <<" ";
else
cout << three[i];
}*/
example:

-----------------------------------Q-------------------------------
------------------------------------------C-------------------------
--------------------------------------------------------------------
-------------D------------------------------------------------------
------------------------------------------------------------------RN
--------------------E--------X-F------------------------------------
---------------------L --------------------------S------------------
----------Y ----------------G-----------------------H---------------
--------------------------------------J------------------K-----------
-----T ----------------I----------------------------------------------
----------P ------------------- O --------------W--------------------
---U----------------------------------V-----------------------------
---------------------------------------------------------------- B----
----------------------------------------- A---------------------------
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>

char random_letter( std::mt19937& rng ) // return random letter in A-Y
{
    static const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXY" ;

    // https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
    static std::uniform_int_distribution<std::size_t> distrib( 0, alphabet.size()-1 ) ;

    return alphabet[ distrib(rng) ] ;
}

template < std::size_t NROWS, std::size_t NCOLS >
void random_fill( char (&grid)[NROWS][NCOLS], std::size_t cnt, char fill_char )
{
    // our random number engine
    // https://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ;

    cnt = std::min( cnt, NROWS*NCOLS ) ; // sanity check

    // fil the first cnt position with random characters, rest with fill_char
    std::size_t nrand = 0 ;
    for( auto& row : grid )
        for( char& c : row )
            c = nrand++ < cnt ? random_letter(rng) : fill_char ;

    // and then randomly shuffle the characters in the grid
    // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( std::begin( grid[0] ), std::end( grid[NROWS-1] ), rng ) ;
}

int main()
{
    char grid[50][10] ;

    random_fill( grid, 100, '-' ) ;

    for( const auto& row : grid )
    {
        for( char c : row ) std::cout << c << "  " ;
        std::cout << "\n\n" ;
    }
}

http://coliru.stacked-crooked.com/a/4a1b83db9cbfece2
Topic archived. No new replies allowed.