Generating two random numbers twice

I am writing a battleship game and I need two random numbers for the location of a ship. But I have two ships to I need to have two random numbers twice. Ofcourse those two pairs of random numbers cannot be the same.

The two functions are almost the same but have different integers. The second function has something extra because it cannot be the same as the first function.

My battleship games consists of a 2D array of 5x5.


The code is writen in Dutch but I translated the comments so you can understand the code.

The problem is that i get an error when i create a second pair.

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
69
70
71
  void LocatieBoot1_Tegenstander(){ //First ship
	srand((unsigned)time(0));
	LocatieBoot1_Rij_C = (rand()%4)+1;	//Generates a number						
	LocatieBoot1_Kolom_C = (rand()%4)+1;	//Generates a second number				
	cout << "Locatie Computer: ";
	cout << LocatieBoot1_Rij_C << " " << LocatieBoot1_Kolom_C << endl;	//Prints those numbers												

	RichtingBoot1_C = (rand()%1)+1;				//The ships have a length of two so they need to be placed horizontal or vertical (0 = hor., 1 = ver.)				
	cout << "Richting Computer: " << RichtingBoot1_C << endl;  //Prints direction

	if (RichtingBoot1_C == 1){ //Horitzontal
		ExtraLengteBoot1_C = (LocatieBoot1_Rij_C + 1); //Extra length is added
		if (ExtraLengteBoot1 == 5){ //Length can not be longer than the length of the 2D array
			ExtraLengteBoot1_C = (LocatieBoot1_Rij_C - 1);      
			LocatieBoot1_Rij_C_Extra = ExtraLengteBoot1_C;
			LocatieBoot1_Kolom_C_Extra = LocatieBoot1_Kolom_C;
			cout << "Extra Locatie ";
			cout << LocatieBoot1_Rij_C_Extra << " " << LocatieBoot1_Kolom_C_Extra << endl;

		}
	}
	else{  //Veritacal
		ExtraLengteBoot1_C = (LocatieBoot1_Kolom_C + 1);
			if (ExtraLengteBoot1 == 5){
				ExtraLengteBoot1_C = (LocatieBoot1_Kolom_C - 1);
				LocatieBoot1_Kolom_C_Extra = ExtraLengteBoot1_C;
				LocatieBoot1_Rij_C_Extra = LocatieBoot1_Rij_C;
				cout << "Extra Locatie ";
				cout << LocatieBoot1_Rij_C_Extra << " " << LocatieBoot1_Kolom_C_Extra << endl;
			}
	}

}

void LocatieBoot2_Tegenstander(){ //Second ship
	srand((unsigned)time(0));
	LocatieBoot2_Rij_C = (rand()%4)+1;		//Generates a random number					
	LocatieBoot2_Kolom_C = (rand()%4)+1;		//Generates a second number				
	if ((LocatieBoot2_Rij_C == LocatieBoot1_Rij_C || LocatieBoot2_Kolom_C ==  LocatieBoot1_Kolom_C)){  //Tests if the numbers from the first ship are the same. The is were the problem begins
		LocatieBoot2_Tegenstander();
	}

	cout << "Locatie Computer: ";
	cout << LocatieBoot2_Rij_C << " " << LocatieBoot2_Kolom_C << endl;													

	RichtingBoot2_C = (rand()%1)+1;		//Again directon ver. or hor.						
	cout << "Richting Computer: " << RichtingBoot2_C << endl;

	if (RichtingBoot2_C == 1){
		ExtraLengteBoot2_C = (LocatieBoot2_Rij_C + 1);
		if (ExtraLengteBoot2 == 5){
			ExtraLengteBoot2_C = (LocatieBoot2_Rij_C - 1);
			LocatieBoot2_Rij_C_Extra = ExtraLengteBoot2_C;
			LocatieBoot2_Kolom_C_Extra = LocatieBoot2_Kolom_C;
			cout << "Extra Locatie ";
			cout << LocatieBoot2_Rij_C_Extra << " " << LocatieBoot2_Kolom_C_Extra << endl;

		}
	}
	else{
		ExtraLengteBoot2_C = (LocatieBoot2_Kolom_C + 1);
			if (ExtraLengteBoot2 == 5){
				ExtraLengteBoot2_C = (LocatieBoot2_Kolom_C - 1);
				LocatieBoot2_Kolom_C_Extra = ExtraLengteBoot2_C;
				LocatieBoot2_Rij_C_Extra = LocatieBoot2_Rij_C;
				cout << "Extra Locatie ";
				cout << LocatieBoot2_Rij_C_Extra << " " << LocatieBoot2_Kolom_C_Extra << endl;
			}
	}

}
It would be easier to use pointer's for the 2 variable's below:

LocatieBoot1_Rij_C
LocatieBoot1_Kolom_C

Unless they are global variable's, LocatieBoot2_Tegenstander() would not
be able to access them.
All variables are global.

And why would it be easier to use pointers??
Last edited on
You're calling srand() too often. It should be called just the once, at progam startup.

Every time you call srand() you are setting the seed. As you use time() for seeding, which is accurate to the second, if you call srand() again quickly enough it will be set to the same value which means that the number returned by the following calls to rand() will be identical.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
using namespace std;

void rand_vals(int seed, int count) {
	cout << "[seed = " << seed << "]" << endl;
	srand(seed);
	// rand vals between 1 and 10
	for(int index = 0; count > index; ++index)
		cout << ((rand() % 10) + 1) << endl;
	cout << endl;
}

int main() {
	rand_vals(1, 4);
	rand_vals(2, 4);
	rand_vals(1, 4);
	rand_vals(3, 4);
	rand_vals(1, 4);

	return 0;
}


Ouput:

[seed = 1]
2
8
5
1

[seed = 2]
6
7
9
6

[seed = 1]
2
8
5
1

[seed = 3]
9
7
5
2

[seed = 1]
2
8
5
1
Last edited on
Thanks! It works now
Op z'n nederlands nog wel, jij durft :P.

Anyways; I believe you should refrain from using std::rand/std::srand. Afaik they're linear RNGs that use globals. Instead, why not use the newer, more C++ styled std::mt19937, in combination with the std::uniform_*_distribution.

http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
Topic archived. No new replies allowed.