Choosing random numbers

Hi all, I'm new here, also new to C++. I would like to ask. I got a task to choose two random numbers for each row. After done some revision, I had successfully solved the problem. But, how can I choose any two random numbers except number 0, 1 and 2 for each row. Any suggestion/ help much appreciated. Thank you in advance :)

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 <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int route[3][7] = {
	{ 0,  4,  5,  6,  7,  1,  0 } ,
	{ 0,  8,  9,  2,  10,  11,  0 } ,
	{ 0,  12,  13,  14,  3,  15,  0 }
};

	srand(time(NULL));

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 7; j++)
			cout << setw(3) << route[i][j] << ' ';
		cout << '\n';
	}
	cout << '\n';

	for (int i = 0; i < 3; i++)
	{
		// Generate two different random numbers
		int r1, r2;
		do
		{
			r1 = rand() % 7;
			r2 = rand() % 7;
		} 
		while (r1 == r2);

		// Ensure r1 is less than r2; if not, swap them.
		if (r1 > r2)
		{
			int temp = r1;
			r1 = r2;
			r2 = temp;
		}

		cout << "For row " << i << ", positions "
			<< r1 << " and " << r2 << " were chosen.\n";

	}
	cout << '\n';
	
}
Last edited on
http://www.cplusplus.com/reference/random/

rand() is C-ish and not preferred anymore. The preferred tool is above.
If you look in the examples under that, you will see you can define some of the engines to do a range, thus avoiding the numbers you do not want (at least for a simple case like your example).

for rand, if you wanted to do it, you have rand%7 which is 0-6.
if you want 3-6 instead, you can do this:
rand()%4 +3 (0,1,2,3 + 3 -> {3,4,5,6})
but again, rand is poorish, so expect to see less than random stuff like 4,4,4,4 from time to time.
Last edited on

rand() is C-ish and not preferred anymore. The preferred tool is above.

Okay.. i will look into it..


if you want 3-6 instead, you can do this:
rand()%4 +3 (0,1,2,3 + 3 -> {3,4,5,6})

I would like to ask.. if i do rand()%4+3, it will select random numbers based on index right? in which from col 4 to col 7.

But, how can i choose two random numbers based on numbers in each row exclude number 0, 1 and 2.

For example:
Row 0: (0, 4, 5, 6, 7, 1, 0)
Two random numbers for row 0 should be either 4, 5, 6 or 7.

Oh.. a bit confusing.. sorry.. hopefully you understand what i mean.

Btw.. thank you for your suggestion above :)

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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <random>
#include <iomanip>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    const int route[6][7] =
    {
        { 0,   4,   5,   6,   7,   1,  0 },
        { 0,   8,   9,   2,  10,  11,  0 },
        { 0,   1,   1,   2,  18,   0,  1 },
        { 0,   1,  13,   2,   3,  15,  0 },
        { 0,   1,   1,   2,   1,   0,  2 },
        { 99, 88,   1,  77,   2,   0, 66 }
    };

    // select two random numbers other than { 0, 1, 2 } from each row

    // initialise a random number engine
    // https://en.cppreference.com/w/cpp/numeric/random
    std::mt19937 rng( std::random_device{}() ) ;

    // for each row in the route
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& row : route )
    {
        // print the items in the row
        std::cout << "row [" ;
        for( int v : row ) std::cout << std::setw(3) << v ;
        std::cout << " ]" ;

        // select two random numbers other than { 0, 1, 2 } from this row

        // 1. make a copy of this row
        // https://cal-linux.com/tutorials/vectors.html
        std::vector<int> cpy( std::begin(row), std::end(row) ) ;

        // 2. shuffle the copy randomly
        // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
        std::shuffle( std::begin(cpy), std::end(cpy), rng ) ;

        // 3. pick the first two values in the shuffled copy that are not in { 0, 1, 2 }
        std::cout << "  select " ;
        int nselected = 0 ;
        for( int v : cpy )
        {
            if( v != 0 && v != 1 && v != 2 )
            {
                if( nselected == 1 ) std::cout << " and " ;
                std::cout << v ;
                ++nselected ;
                if( nselected == 2 ) break ;
            }
        }

        if( nselected == 0 ) std::cout << "nothing (did not find any suitable number)\n" ;
        else if( nselected == 1 ) std::cout << " (found only one suitable number)\n" ;
        else std::cout << '\n' ; // found two
    }
}

http://coliru.stacked-crooked.com/a/e998a0f83ebbaa90
I would like to ask.. if i do rand()%4+3, it will select random numbers based on index right? in which from col 4 to col 7.

no. All that does is give you a value in a range, it is not tied to your index. I read 'for' literally, and I see now you meant 'from'.

'from' is more work, as shown :)
Topic archived. No new replies allowed.