Inserting a variable randomly into a vector

Hi I am a novice programmer and was trying to insert a variable into a vector using the rand() function. I have written code (below) which prints out a vector containing '.' of 6x20. Now I am trying to insert a variable such as 'T' into it randomly. The commented out code is what I have tried unsuccessfully so far.

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
  #include <iostream>
#include <vector>
using namespace std;

int main()
{
	int i,j;
	int RandomT = rand() % 120;
	
	vector <vector<char>> map(6, vector<char>(20, '.'));
	
	for (i = 0; i < 6; i++) {
		for (j = 0; j < 20; j++)
		{
			cout << map[i][j];
			//if (((i*20) +j) == RandomT){
			    //map[i][j] = 'T';
			}
		}
	

	cout << endl;
	}
	return 0;
}
	
Define "unsuccessful".

1. Perhaps you should change the chosen element before showing it?

2. How about a newline after each row?


PS. It is possible to compute i and j from the RandomT without a loop and if:
i=RT/20
j=RT%20
The code prints out the 6x20 fine. But the code does not randomly place a T within it. Not sure what you mean
I have now managed to insert the variable into the vector succesfully but for some reason it is not random? I was wondering if there was something obvious I was missing. Thanks in advance.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
int i,j;
int RandomT = rand() % 120;

vector <vector<char>> map(6, vector<char>(20, '.'));

for (i = 0; i < 6; i++)
{
for (j = 0; j < 20; j++)
{
cout << map[i][j];

if (((j*20) +i) == RandomT)
{
map[i][j] = 'T';
cout << map[i][j];
}
}

cout << endl;
}

return 0;
}
You never call srand() to seed the random number generator.
http://www.cplusplus.com/reference/cstdlib/srand/

Please ALWAYS use code tags.

Topic archived. No new replies allowed.