rand() gives the same number everytime

hello,

I have a problem with the:
rand();
function.
It keeps generating the same number over and over again.
I use it like 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
switch (mode)
	{
		case 1:
			i = rand() %100;
			cout<<"number between 1 and 100"<<endl;
			x=false;
			break;
		case 2:
			i = rand() %500;
			cout<<"number between 1 and 500"<<endl;
			x=false;
			break;
		case 3:
			i = rand() %2500;
			cout<<"number between 1 and 2500"<<endl;
			x=false;
			break;
		case 4:
			i = rand() %10000;
			cout<<"number between 1 and 10000"<<endl;
			x=false;
			break;
		default:
			cout<<"please choose again"<<endl;
			break;
	}


case 2 gives 277 everytime and case one gives 35 everytime. even if i close the program shut down my pc and then open the program again it gives these numbers. what did I do wrong?

total code
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <fstream>

using namespace std;

struct RecAbbo{
		std::string name;
		std::string surname; 
		int games;
		int won;
		int same;
		int lost;
		int money;
	}Abbo[100];
	void play(int pl1, int pl2);
int main(){
char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2, i = 0;
	bool check = false, check2 = false ;
	while (check == false){	
		i1 = rand() % 100;
		i2 = rand() % 100;
		while (check2 == false) 
			if (i1 == i2){
				i2 = (i2 + rand() % 100) - 100;
			} else {
				if (Abbo[i1].name.empty() == 0 || Abbo[i2].name.empty() == 0){
					check2 = false;
				} else {
					check2 = true;
					check = true;
				}
			}
		}
	while (i < 20){
			name1[i] = Abbo[i1].name[i];
			name2[i] = Abbo[i2].name[i];
			surname1[i] = Abbo[i1].surname[i];
			surname2[i] = Abbo[i2].surname[i];
			
			i++;
	}
	Abbo[i1].games++;
	Abbo[i2].games++;
	cout<<"       player 1:     player 2: \n";
	cout<<"game: "<<name1<<surname1<<" VS :"<<name2<<surname2<<endl;
	play(i1,i2);
}
void play(int pl1, int pl2){
int i, p1,p2,mode,p1money = 50, p2money = 50;
	bool x = true,z = true;
	while (x){
	cout<<"for easy mode press 1 "<<endl;
	cout<<"for normal mode press 2 "<<endl;
	cout<<"for hard mode press 3 "<<endl;
	cout<<"for beast mode press 4 "<<endl;
	cout<<"choise: ";
	cin>>mode;
	switch (mode)
	{
		case 1:
			i = rand() %100;
			cout<<"number between 1 and 100"<<endl;
			x=false;
			break;
		case 2:
			i = rand() %500;
			cout<<"number between 1 and 500"<<endl;
			x=false;
			break;
		case 3:
			i = rand() %2500;
			cout<<"number between 1 and 2500"<<endl;
			x=false;
			break;
		case 4:
			i = rand() %10000;
			cout<<"number between 1 and 10000"<<endl;
			x=false;
			break;
		default:
			cout<<"please choose again"<<endl;
			break;
	}
	while(z){
		cout<<"player 1: ";
		cin>>p1;
		if(p1 < i)
		cout<<"number is higher"<<endl;
		if(p1 > i)
		cout<<"number is lower"<<endl;
		cout<<"player 2: ";
		cin>>p2;
		if(p2 < i)
		cout<<"number is higher"<<endl;
		if(p2 > i)
		cout<<"number is lower"<<endl;
		if (p1 == i){
			cout<<"player 1 has won!!"<<endl;
			Abbo[pl1].won++;
			Abbo[pl2].lost++;
			Abbo[pl1].money = Abbo[pl1].money + p1money;
			cout<<"money won for player 1: "<<p1money<<endl;
			z = false;
			} else {
			p1money=p1money-5;
		}
		if (p2 == i){
			cout<<"player 2 has won!!"<<endl;
			Abbo[pl2].won++;
			Abbo[pl1].lost++;
			Abbo[pl2].money = Abbo[pl2].money + p2money;
			cout<<"money won for player 2: "<<p2money<<endl;
			z = false;
			} else {
			p2money=p2money-5;
		}
		if (p1money == 0 || p2money == 0){
			Abbo[pl1].same++;
			Abbo[pl2].same++;	
			}
		}
	}
}	
you need to call srand() at the top of main:

http://www.cplusplus.com/reference/cstdlib/srand/
so it would be like 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
switch (mode)
	{
		case 1:
			i = rand() %100;
			cout<<"number between 1 and 100"<<endl;
			srand (time(NULL));
			x=false;
			break;
		case 2:
			i = rand() %500;
			cout<<"number between 1 and 500"<<endl;
			srand (time(NULL));
			x=false;
			break;
		case 3:
			i = rand() %2500;
			cout<<"number between 1 and 2500"<<endl;
			srand (time(NULL));
			x=false;
			break;
		case 4:
			i = rand() %10000;
			cout<<"number between 1 and 10000"<<endl;
			srand (time(NULL));
			x=false;
			break;
		default:
			cout<<"please choose again"<<endl;
			break;
	}
Last edited on
so it would be like this???:
No, only once after line 20, before line 23
I dont get that...
srand should only be run once to set your seed, so remove line 6, 12, 18 and 24 from your switch.

At the top of your main() function put 1 srand call.
thanx
Topic archived. No new replies allowed.