# Random numbers issue

Page 1 of 1

## 8 Replies - 533 Views - Last Post: 18 April 2013 - 07:14 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=318775&amp;s=abacd12cc77ec64051df4063417f0da1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Verance

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 17-April 13

# Random numbers issue

Posted 17 April 2013 - 07:54 AM

Hey guys,
I'll not ask you to do my homework I just need you to enlight me.

I did this all by myself but Im having hard times when I try to add a vector that generates numbers in the range I need.

I got this

```#include <iostream>
#include <vector>
#include <cstddef>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

void showVector2D(const vector< vector<int> > &v)

{

for (size_t i=0; i< v.size() ; i++)

{
for (size_t j=0; j<v.size(); j++)

cout << setw(3) << v[i][j];
cout << endl;
}
cout << endl;
}

/*void random (unsigned int)
{
srand((unsigned)time(0));
int tamanho;
int random_integer;
int lowest=0, highest= tamanho*tamanho - 1;
int range=(highest-lowest)+1;

for(int i=0; i<tamanho; i++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl;
}
}*/

int main()
{
int tamanho;
cout << "Qual o tamanho do seu tabuleiro? " << endl;
cin >> tamanho;

if (tamanho == 0)
{

cout << "EMPTY VECTOR ...\n";
return 0;

}
srand((unsigned)time(0));

vector< vector<int> > v1;
for (size_t i=0; i<tamanho; i++)
{
vector<int> v2 (1,2,3);

for (size_t j=0; j <= tamanho; j++)

v2.push_back(10*(i+1)+j);

v1.push_back(v2);

}

//show vector elements

cout << "v1\n"; showVector2D(v1);
}
```

This is a pretty simple board game which the program asks the user for the board size (done already) and then it fills it with numbers between 0 and size*size-1.
How do I create or modify the vector to get it working?

Thanks in advance. I really need this asap.

Regards.

Is This A Good Question/Topic? 0

## Replies To: Random numbers issue

### #2 jimblumberg

Reputation: 5487
• Posts: 17,062
• Joined: 25-December 09

## Re: Random numbers issue

Posted 17 April 2013 - 09:54 AM

Okay, it doesn't look like you really understand vectors so please answer these questions.

Please explain what you think the following line is actually doing?
```        vector<int> v2 (1,2,3);
```

You may want to study the documentation for the vector constructor. And you probably just want:

```vector<int> v2;
```

Jim
Was This Post Helpful? 0

### #3 Verance

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 17-April 13

## Re: Random numbers issue

Posted 17 April 2013 - 09:58 AM

Well, it seems like I actually understand something since I was able to fix it by my self. That (1,2,3) was just some random test.

```vector< vector<int> > v1;
for (size_t i=0; i<tamanho; i++)
{
vector<int> v2;

for (size_t j=0; j <= tamanho; j++)

v2.push_back((i*tamanho)+j);

v1.push_back(v2);
```

I did like this but now I gotta shuffle everything so I tried this:
```int main()
{
int tamanho;

cout << "Qual o tamanho do seu tabuleiro? " << endl;
cin >> tamanho;

if (tamanho == 0)
{

cout << "EMPTY VECTOR ...\n";
return 0;

}
srand((unsigned)time(0));

vector<int> random_vec;

random_shuffle(random_vec.begin(), random_vec.end());

for (int i = 0; i< tamanho*tamanho; i++)
random_vec[i] = i;

vector< vector<int> > v1;
for (size_t i=0; i<tamanho; i++)
{
vector<int> v2;

for (size_t j=0; j <= tamanho; j++)

v2.push_back(random_vec[(i*tamanho)+j]);

v1.push_back(v2);

}
```

it says its out of range and I cant figure out.
I'm a newbie anyways, no need to be that rude. Asking for help so I can improve my skills.

Thanks for your help.

Regards
Was This Post Helpful? 0

### #4 jimblumberg

Reputation: 5487
• Posts: 17,062
• Joined: 25-December 09

## Re: Random numbers issue

Posted 17 April 2013 - 10:08 AM

Quote

That (1,2,3) was just some random test.

That was exactly my point, throwing some random code at a problem will rarely succeed. You need to find and read the documentation for the functions/classes you're trying to use so you can properly use them. If you think I'm being rude telling you to read the documentation, then I sorry but that's life. To learn to program you need to learn to find, read, and understand the documentation.

Quote

it says its out of range and I cant figure out.

You can't shuffle an empty vector, try the shuffle after you insert some elements into the array. Also you can't use the array notation[] to access vector elements until there are elements in the vector. You can't assign a value to a non-existent element. Again read the documentation for the vector class.

Jim
Was This Post Helpful? 2

### #5 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Random numbers issue

Posted 17 April 2013 - 10:34 PM

Your vector is two dimentional. v.size() is the number of vectors in the array, v[n].size() is the size of the vector at index n.

```void showVector2D(const vector< vector<int> > &v)

{

for (size_t i=0; i< v.size() ; i++)

{
for (size_t j=0; j<v.size(); j++)  //<<< should be v[i].size()

cout << setw(3) << v[i][j];
cout << endl;
}
cout << endl;
}

```

Was This Post Helpful? 0

### #6 Verance

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 17-April 13

## Re: Random numbers issue

Posted 18 April 2013 - 04:50 AM

Alright, thank you for your replies

I got my code stable atm and it is like this

```#include <iostream>
#include <vector>
#include <cstddef>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

void showVector2D(const vector< vector<int> > &v)

{

for (size_t i=0; i< v.size() ; i++)

{
for (size_t j=0; j<v.size(); j++)

cout << setw(3) << v[i][j];
cout << endl;
}
cout << endl;
}

int main()

{

int tamanho;

cout << "Qual o tamanho do seu tabuleiro? " << endl;

cin >> tamanho;

if (tamanho == 0)

{

cout << "EMPTY VECTOR ...\n";

return 0;

}

srand((unsigned)time(0));

cout << "Passei aqui 1" << endl;

vector<int> random_vec;

random_shuffle(random_vec.begin(), random_vec.end());

cout << "Passei aqui 2" << endl;

cout << "Passei aqui " << tamanho*tamanho << endl;

for (int i = 0; i < tamanho*tamanho; i++){

random_vec.push_back(i);

random_vec;

}

cout << "Passei aqui 3" << endl;

vector< vector<int> > v1;

for (size_t i=0; i<tamanho; i++)

{

vector<int> v2;

for (size_t j=0; j <= tamanho; j++)

v2.push_back((i*tamanho)+j);

v1.push_back(v2);

cout << "Passei aqui 4" << endl;

}

//show vector elements

cout << "v1\n"; showVector2D(v1);

}
```

I just can't get that vector shuffle to work.

```for (int i = 0; i < tamanho*tamanho; i++){

random_vec.push_back(i);

random_vec;

}
```

(Tamanho means size)

How do manage to put this vector on my vector of vectors?

Thanks in advance

Regards
Was This Post Helpful? 0

### #7 jimblumberg

Reputation: 5487
• Posts: 17,062
• Joined: 25-December 09

## Re: Random numbers issue

Posted 18 April 2013 - 06:39 AM

Let's start by looking at several things in your code.

First this snippet:
```	vector<int> random_vec;

random_shuffle(random_vec.begin(), random_vec.end());

```

The first line creates an empty vector of ints, the second tries to sort this empty vector. As I said earlier it doesn't make sense to shuffle an empty vector, you need to populate the vector first. You need to move that second line to somewhere after you populate the vector (Line 75 would be a good place).

Next snippet:

```	for (int i = 0; i < tamanho*tamanho; i++){

random_vec.push_back(i);

random_vec;

}
```

This where you are actually populating your vector. The second line actually inserts i into the back of the vector. The random_vec; doesn't actually do anything so it can be removed.

Also since you never use random_vec anywhere what is the actual purpose of this vector and all the operations you are doing to this vector?

Next snippet:

```	vector< vector<int> > v1; // Create an empty vector<vector<int>> named v1.

for (size_t i=0; i<tamanho; i++)
{
vector<int> v2;  // Create an empty vector<int> named v2.
for (size_t j=0; j <= tamanho; j++)
v2.push_back((i*tamanho)+j); // Insert a value into v2.

v1.push_back(v2); // Insert a value into v1.

cout << "Passei aqui 4" << endl;
}
```

In this snippet you are creating a vector<vector> v1 this vector will have a size of v1[tamanho][tamanho+1]. So if I entered 5 for the value of tamanho this vector will have a size of 5 for the outer vector and 6 for the inner vector.

The reason your inner vector will have a size of tamanho + 1 is because you are using the operator<= instead of operator<.

Next your display routine is not properly displaying your vector:

```	for (size_t i=0; i< v.size() ; i++) // This is correct.
{
for (size_t j=0; j<v.size(); j++) // This is incorrect.
cout << setw(3) << v[i][j];
cout << endl;
}
cout << endl;
}
```

The outer loop is correct, you'll go thru each element of the outer vector. The second loop is incorrect. Here you should be using the size of the inner vector, not the outer vector. It should be using v[i].size() instead of v.size().

This is the contents of the vectors when you enter a value of 5 for tamanho.

```  0  1  2  3  4  5
5  6  7  8  9 10
10 11 12 13 14 15
15 16 17 18 19 20
20 21 22 23 24 25

```

Jim
Was This Post Helpful? 0

### #8 Verance

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 17-April 13

## Re: Random numbers issue

Posted 18 April 2013 - 06:51 AM

Thank you very much for the effort.

I could make it work and randomly, I'm just gonna show how I did it.

Well, my main error was in the showVector2D function as you mentioned

```void showVector2D(const vector< vector<int> > &v)

{

for (size_t i=0; i< v.size() ; i++)

{
for (size_t j=0; j<v[i].size(); j++) {

if(v[i][j] == 0)
cout << setw(3) << " ";
else
cout << setw(3) << v[i][j];

}
cout << endl;
}
cout << endl;
}
```

I was doing it wrong and I was trying to get an invalid argument, indeed.

Second error was my vector shuffle code line, I just figured it out after retyping the algorithm.

Third and this one was hard to find was this:

```for (size_t i=0; i<tamanho; i++)

{

vector<int> v2;

for (size_t j=0; j <[b](=)[/b] tamanho; j++){

v2.push_back(random_vec[(i*tamanho)+j]);
cout << "valor random vec" << random_vec[(i*tamanho)+j] << endl;

}

v1.push_back(v2);

cout << "Passei aqui 4" << endl;

}
```

I had an extra "=" in that function so I was overextending the range.

Yes, you're right. I'm actually lacking some experience and some theoric knowledge. It's like I'm working in a "brute-force" mode. I really appreciate your help and the way you tried to explain me those basic things.

I still got a lot to do and I hope I can make some reasonable questions in future.

I'll need to start the game now. I got my mixed board and 0 is my "free spot". Now its time to ask the player which numbers he wants to move and where to. I still have no idea how I'm gonna start but I'll sort it out.

Thank you.

Best regards.

Luís.
Was This Post Helpful? 0

### #9 jimblumberg

Reputation: 5487
• Posts: 17,062
• Joined: 25-December 09

## Re: Random numbers issue

Posted 18 April 2013 - 07:14 AM

The following line will probably result in a program crash.
```random_vec[(i*tamanho)+j] << endl;
```

How many elements does the vector random_vec contain? Does it contain (tamanho* tamanho) + tamanho elements, the important part here is that addition operation.

Jim
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }