random number generator between 0 and 100

hello

I have a random number generator that should genetate 2 numbers between 100 and 0.
the first number works fine but the second doesn't, why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
i1 = random() %100;
	i2 = random() %100;
	while(i1 < 0 && i1 > 100){
		if (i1 < 0)
			i1 = i1 + random() %100;
		if (i1 > 100)
			i1 = i1 - random() %100;
		}
	while(i2 < 0 && i2 > 100){
		if (i2 < 0)
			i2 = i2 + random() %100;
		if (i2 > 100)
			i2 = i2 - random() %100;
		}
	if(i2 == i1)
		i2 = i2 + random() %100;


can you give me tips on how to do this.
What is "does not look fine?"

Both while loops are unnesesary because both numbers can not be less than 0 or larger than 99.

What is the purpose of the if on line 25?

And most important: what is the random() function? It can be a source of all problems here.
on my screen it only goes to line 16 and does not look fine isent mentiont, shall I report the comment
25, 15
That one.

What is the problem? What do you mean "not looks fine"?
Are they are not in 0..100 range? Are they crash your program? Are they looks like garbage on output? Is it something else?

I run your code replacing non-standard random() by rand() and everything is fine.

Only problem is that your if on line 15 cam force i2 to be out of range 0..100, but it should happens one out of 100 tries. Solution to this problem depends on purpose of that if here.
the first one gives an output between 1 and 100 but the second one doesn't it gives between 1000 and -200 from what i've noticed.
Ok. even assuming improper and malign implementation of random(), i2 should still not get out of [-99, 198] range.

I believe that eror lies elsewhere in your code. Yf you post it we will be able to fix this problem.

By the way, both while loops are not only unnessesary, they are useless. Condition (i1 < 0 && i1 > 100) will never be true because number cannot be less than 0 and larger than 100 at the same time.

What is the purpose of last if here? DO you want to generate two different random numbers? Or is it something other?
they have to be different yes

total code: (WIP)

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <time.h>

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);
void game(int i);
int add(int i);
int main(){
	srand (time(NULL));
	int i = 1,choise;
	bool exit = false;
	while (!exit){
		cout<<"1: add player  \n";
		cout<<"2: play a game  \n";
		cout<<"0: exit  \n";
		cout<<"choose: ";
		cin>>choise;
		switch (choise){
			case 1:
				i = add(i);
				break;
			case 2:
				game(i);
				break;
			case 3:
				exit = true;
				break;
			default:
				cout<<"please rechoose \n";
		}
	}
}
int add(int i){
	cout<<"name: ";
	cin>>Abbo[i].name;
	cout<<"surname: ";
	cin>>Abbo[i].surname;
	Abbo[i].games = 0;
	Abbo[i].won = 0;
	Abbo[i].same = 0;
	Abbo[i].lost = 0;
	Abbo[i].money = 0;
	i++;
	return i;
	}
void game(int i){
	cout<<"test ";
	char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2,j = 0;
	i1 = rand() %100;
	i2 = rand() %100;
	while(i1 < 0 && i1 > 100){
		if (i1 < 0)
			i1 = i1 + rand() %100;
		if (i1 > 100)
			i1 = i1 - rand() %100;
		}
	while(i2 < 0 && i2 > 100){
		if (i2 < 0)
			i2 = i2 + rand() %100;
		if (i2 > 100)
			i2 = i2 - rand() %100;
		}
	while(i2 == i1)
		i2 = i2 + rand() %100;
		i2 = i2 - rand() %100;
	cout<<i1<<"  "<<i2;
}

void play(int pl1, int pl2){
int i, p1,p2,mode,p1money = 50, p2money = 50;
	srand (time(NULL));
	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;
		cout<<"player 2: ";
		cin>>p2;
		if(p1 < i)
		cout<<"number player 1 is higher"<<endl;
		if(p1 > i)
		cout<<"number player 1 is lower"<<endl;
		if(p2 < i)
		cout<<"number player 2 is higher"<<endl;
		if(p2 > i)
		cout<<"number player 2 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++;	
			}
		}
	}
}	
1
2
3
	while(i2 == i1)
		i2 = i2 + rand() %100;
		i2 = i2 - rand() %100;
Third line is not in while loop. It will be executed exactly once after the loop. Even if it was within a loop, i2 still can go out of 0..99 range.

You can simply reroll second number until it is not equal to first.

Here is boiled down example of your program which generates two distinct numbers in 0..100 range:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void game()
{
	cout<<"test ";
	int i1 = rand() % 101;
	int i2 = rand() % 101;
	while(i2 == i1)
		i2 = rand() % 101;
	cout<<i1<<"  "<<i2<<'\n';
}

int main()
{
    srand(time(nullptr));
    for(int i = 0; i < 100; ++i)
        game();
}
http://ideone.com/J3uGnj
Last edited on
A possible improvement to MiiNiPaa's code:
1
2
3
4
5
6
7
8
9
10
void game()
{
	int i1, i2;
	cout<<"test ";
	i1 = rand() % 101;
	do {
		i2 = rand() % 101;
	} while(i2 == i1);
	cout<<i1<<"  "<<i2<<'\n';
}
Well, both code snippets are identical perfomance-wise, so it is a matter of preference.
Another improvement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void game()
{
    int i1, i2;
    cout<<"test ";
    i1 = rand() % 101;
    i2 = rand() % 100;
    if (i2==i1)
    {
        // Ooops, i1 and i2 are equal.  The real i2 must be 100, which
        // is not a possible result of rand() % 100.
        i2=100;
    }
    cout<<i1<<"  "<<i2<<'\n';
}

There are 101 possible choices for i1. Since i2 can't match i1, there are only 100 possible choices for i2 for each possible i1.
Last edited on
It is a good improvement which saves distribution probabilities, but nesseties a comment and might be not understandable for people without math background.
I like it.

Another (non-optimal) solution. just for fun:
1
2
3
4
5
6
7
8
void game()
{
    std::cout << "test ";
    int n[101];
    std::iota(n, n + 101, 0);
    std::random_shuffle(n, n + 101);
    std::cout << n[0] << "  " << n[1] << '\n';
}
Last edited on
Well, both code snippets are identical perfomance-wise, so it is a matter of preference.

The improvement is code size since it avoids the duplicate i2 = rand() % 101;.
Sure thing. A single call statement, one div and possibly one move. How much is that? Less than 16 bytes. I call it premature optimisation. Perfomance-wise it is identical (Actually there is a possibility that compiler will optimise it and will not generate second call).
thanks forr all the solutions, I'll see whitch one fits me best, no i want to check if the number is lower than a variable called i, can i just add a while loop for both rand() statements in MiiNiPaa code
Last edited on
line 67 blows my mind. how can i1 be less than 0 and greater than 100?
it was for some reason without the loop. just like i2 was before
Topic archived. No new replies allowed.