Random Positions in 2d array

Hi.

How do generate 15 random positions in a 2d array and store 'F'.

Example:

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main()
{
char bloodyBoard[10][10];
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
bloodyBoard[i][k] = 'E'; // store 'E'
cout << bloodyBoard[i][k] << " "; // display board
}
cout << endl;
}
}

I set the 10x10 array to store 'E'.
But afterwards to RNG 15 positions to store 'F' I'm stuck.
Please help!
This is one way:
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
49
50
51
#include <iostream> 
#include <numeric>
#include <iterator>
#include <algorithm>
#include <random>

int main()
{
    constexpr std::size_t SZ = 10 ;
    char board[SZ][SZ] ;

    // fill with '.'
    // http://www.stroustrup.com/C++11FAQ.html#auto
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( auto& row : board ) for( char& c : row ) c = '.' ;

    std::size_t REQUIRED_CELLS = 15 ;

    // fill up an array with positions 0 .. SZ*SZ-1
    std::size_t positions[SZ*SZ] ;
    // http://en.cppreference.com/w/cpp/algorithm/iota
    // http://en.cppreference.com/w/cpp/iterator/begin
    std::iota( std::begin(positions), std::end(positions), 0 ) ;

    // shuffle it randomly
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    // http://en.cppreference.com/w/cpp/numeric/random
    std::shuffle( std::begin(positions), std::end(positions),
                  std::mt19937( std::random_device {}() ) ) ;

    // pick the first REQUIRED_CELLS positions in the shiffled sequence
    for( std::size_t i = 0 ; i < REQUIRED_CELLS ; ++i )
    {
        board[0][ positions[i] ] = '*' ;

        // or, in long hand:
        /*
        const auto pos = positions[i] ;
        const auto row = pos / SZ ;
        const auto col = pos % SZ ;
        board[row][col] = '*' ;
        */
    }

    // print the board
    for( const auto& row : board )
    {
        for( char c : row ) std::cout << c << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/93b0253b3bcd66a6

This is another (to me, more elegant) way:
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
#include <iostream> 
#include <random>

int main()
{
    constexpr std::size_t SZ = 10 ;
    char board[SZ][SZ] ;

    // fill with '.'
    // http://www.stroustrup.com/C++11FAQ.html#auto
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( auto& row : board ) for( char& c : row ) c = '.' ;

    std::size_t required_cells = 15 ;
    std::size_t available_cells = SZ*SZ ;
    std::mt19937 rng( std::random_device {}( ) ) ;

    for( auto& row : board ) for( char& c : row ) // for each cell, one by one
    {
        if( required_cells > 0 ) // if there are more celles to be picked
        {
            // pick this cell with a probability of required_cells / available_cells  
            // ie. with our example of 100 cells, pick 15
            // pick board[0][0] with a probability opf 15/100
            // pick board[0][1] with a probability opf (15/100) * (14/99) + (85/100) * (15/99)
            //      ==   (15*14)/9900 + (85*15) / 9900  ==  1485/9900 == 15/100 
            // and so on.
            if( std::uniform_int_distribution<std::size_t>( 0, available_cells - 1 )( rng ) < required_cells )
            {
                c = '*' ;
                --required_cells ;
            }
            --available_cells ;
        }
    }

    // print the board
    for( const auto& row : board )
    {
        for( char c : row ) std::cout << c << ' ' ;
        std::cout << '\n' ;
    }
}

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