Generating two numbers randomly for each row

Jan 15, 2019 at 6:23am
Hi everyone, I got a task which need to randomly choose two numbers for each row[i]. The random chosen numbers must not be 0, 1 and 2, also not similar values for both random number. The data is in 2d array. I had try but when i run for a several time, sometimes appear same number for both chosen random number, also number that is not in the array.

Sorry for asking. I'm new to C++. I would like to ask. For example, if the randomly chosen numbers is 8 and 10. How can i move 10 beside 8. Is there any way to do it.

Example row[0]={ 0, 4, 8, 9, 5, 11, 10, 12, 7, 3, 6, 0, 0 }

The answer should be row[0] = { 0, 4, 8, 10, 9, 5, 11, 12, 7, 3, 6, 0, 0 }

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
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>

using namespace std;

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

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

	// choose two different values randomly from each row
	srand (time(NULL)); //initialize the random seed

	for (int i = 0; i < 3; i++)
	{
			int RandValue1 = 3 + (rand() % 12);
			int RandValue2 = 3 + (rand() % 12);
			cout << "row: " << i << endl;
			cout << "RandValue1: " << RandValue1 << endl;
			cout << "RandValue2: " << RandValue2 << endl;
	}
	system("pause");
	return 0;
}
Jan 15, 2019 at 6:31am
I can't understand your question. Where is this row array? What does route have to do with it? I don't understand your "example" either.
Jan 15, 2019 at 6:47am
I got two question here.

First: I want to randomly choose two values from this list below for each row.

{{ 0, 4, 8, 9, 5, 11, 10, 12, 7, 3, 6, 0, 0 } ,
{ 0, 10, 11, 9, 8, 5, 4, 7, 3, 6, 12, 0, 0 } ,
{ 0, 0, 8, 9, 5, 4, 11, 10, 12, 7, 3, 6, 0 }}

I had try with the code and run several time. Sometimes appear number i don't want. The random chosen number should be between 3-12 (not 0, 1 and 2). The chosen number 1 and chosen number 2 can't be the same number.

Second question:
For example, if the randomly chosen numbers is 8 and 10. How can i move 10 beside 8. Is there any way to do it.


{ 0, 4, 8, 9, 5, 11, 10, 12, 7, 3, 6, 0, 0 } ,

How can I move 10 beside 8. I don't have idea to do it.

The answer should be

{ 0, 4, 8, 10, 9, 5, 11, 12, 7, 3, 6, 0, 0 } ,

Hopefully you understand what i'm trying to ask.
Jan 15, 2019 at 7:48am
I think I understand what you are trying to do. How about this:

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
65
66
67
68
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

const int Rows = 3, Cols = 13;

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

int main()
{
    int route[Rows][Cols] = {
        { 0,  4,  8,  9,  5, 11, 10, 12,  7,  3,  6,  0,  0 } , 
        { 0, 10, 11,  9,  8,  5,  4,  7,  3,  6, 12,  0,  0 } , 
        { 0,  0,  8,  9,  5,  4, 11, 10, 12,  7,  3,  6,  0 } };

    srand(time(NULL));

    print(route);

    for (int row = 0; row < Rows; row++)
    {
        // Find first non-zero entry in current row.
        int col = 0;
        for ( ; col < Cols && route[row][col] == 0; ++col)
            ;

        // Generate two different random ints from col to col + 9, inclusive.
        int i1, i2;
        do
        {
            i1 = rand() % 10 + col;
            i2 = rand() % 10 + col;
        }
        while (i1 == i2);

        // Ensure i1 is less than i2; if not, swap them.
        if (i1 > i2)
        {
            int tmp = i1;
            i1 = i2;
            i2 = tmp;
        }
        
        cout << "For row " << row << ", positions "
             << i1 << " and " << i2 << " were chosen.\n";

        // Move value at position i2 next to value at position i1.
        int val = route[row][i2];        
        for ( ; i2 > i1 + 1; --i2)
            route[row][i2] = route[row][i2 - 1];
        route[row][i2] = val;
    }

    cout << '\n';
    print(route);
}

Jan 15, 2019 at 9:43am
Thanks @Dutch for your help.

I would like to ask.
1
2
3
4
5
6
 int i1, i2;
        do
        {
            i1 = rand() % 10 + col;
            i2 = rand() % 10 + col;
        }


why do we need to
+col
? It is similar if replace
+col
with
+1
.


Jan 15, 2019 at 10:05am
rand() % 10 + 1; will give you a random number in the range [1,10]
rand() % 10 + col; will give you a random number in the range [0+col,9+col]

Since col's value isn't always '1' and changes, no they're not the same. col, if you notice, is being incremented by the for-loop.
Jan 15, 2019 at 10:17am
Hi Grime,

rand() % 10 + col;

Thus this code mean it will give me a random number between column[0] to column[10]
Jan 15, 2019 at 4:00pm
Are you asking a question? I can't tell.

rand() % 10 + cal, as Grime said, gives a random number in the inclusive range [0 + col, 9 + col].
Let's say col == 2.
rand() % 10 + 2 will gives you a random number in the range [2, 11], inclusive.
Jan 15, 2019 at 4:17pm
Hi Ganado,


Let's say col == 2.
rand() % 10 + 2 will gives you a random number in the range [2, 11], inclusive.


Now I could understand the different between those. Btw, thanks for your explanation.
Jan 15, 2019 at 4:19pm
Glad it helped.
Jan 15, 2019 at 4:47pm
yat89 wrote:
Is it similar if replace +col with +1 ?

It would give the same result for the first two rows, which both have their first non-zero value at position 1. But the third row has its first non-zero value at position 2.

I noticed that your rows have the values from 3 to 12 (10 values) that are positioned contiguously but start at different offsets. You seemed to want to choose two of those values randomly and shift the rightmost over so it is just to the right of the other. I have no idea why you want to do that, but that's what it seemed you wanted.

Note that if the two random positions are already next to each other then there will be no change. Since I have no idea what you are up to, I have no idea if that's a problem.
Last edited on Jan 15, 2019 at 4:48pm
Jan 15, 2019 at 5:15pm
Hi dutch..

Yes, you're right. I need to choose two values randomly between 3 to 12. Then, place the second random value next to the first random value. Its hard to tell u why i need to do that because the task was given that way.

Btw, thank you so much for your help.
Topic archived. No new replies allowed.