Max and Min Random Generate Number

My problem : My random generator keep generate 0. Anyway to fix 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
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
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

void game();
void menu_Select();

int choice;

void game()
{
	srand((unsigned int)time(NULL));

	//declare variables
	int min_num=0; int max_num=0;
	int range = (max_num - min_num) + 1;
	int rand_gen = rand() % range + min_num;
	
	//game logo & intro
	cout << "\t\tWelcome to GUESSING MAXIMUM AND MINIMUM GAME !!\n";
	cout << "Please enter number between your range. Are you up to the challenge ?!\n";
	cout << endl;
	cout << "-----------------****GUESSING MAXIMUM AND MINIMUM GAME*****------------------------------" << endl;

	while (true)
	{
		cout<<"Please key in the minimum value and maximum value for the range of the random number.\n";
		cin >> min_num >> max_num;
		cin.ignore();

		//promt error message
		while (max_num < min_num)
		{
			cout << "So sorry. Please re-enter your minimum and maximum value.\n";
			cout << "Enter your value : ";
			cin >> min_num >> max_num;
			cin.ignore();
		}

		//check maximum and minimum value

		if (rand_gen % 2 == 0 && rand_gen > 0)
		{
			cout << "The first number generated is " << rand_gen << " It is an even positive number" << endl;
		}
		else
		{
			cout << "The first number generated is " << rand_gen << " It is an odd positive number" << endl;
		}
		if (rand_gen % 2 == 0 && rand_gen < 0)
		{
			cout << "The first number generated is " << rand_gen << " It is an even negative number" << endl;
		}
		else
		{
			cout << "The first number generated is " << rand_gen << " It is an odd negative number" << endl;
		}
		if (rand_gen == 0)
		{
			cout << "The number generated is " << rand_gen << endl;
		}
	}
	menu_Select();
}

void menu_Select()
{
	do
	{
		cout << "*****QUESTION*****" << endl;
		cout << "1-Play" << endl;
		cout << "2-Exit" << endl;
		cout << "Your choice : ";
		cin >> choice;
		cout << "\n\n";
	} while (choice < 1 || choice > 2);

	if (choice == 1)
	{
		game();
	}
}

int main()
{
	menu_Select();
    return 0;
	
}



SAMPLE OUTPUT

Example 1
Please key in the minimum value and maximum value for the range of the random number:
-10 10
The first number generated is 7. It is an odd positive number.
The second number generated is -3. It is a negative number.

Example 2
Please key in the minimum value and maximum value for the range of the random number:
-10 20
The first number generated is 0.
The second number generated is 18. It is an even positive number.
1
2
3
4
	//declare variables
	int min_num=0; int max_num=0;
	int range = (max_num - min_num) + 1;
	int rand_gen = rand() % range + min_num;

So this part in lines 19-21, should just be
int min_num, max_num;
(delete everything else)

Then around line 43 you want
1
2
int r1 = rand() % (max_num-min_num+1) + min_num;
int r2 = rand() % (max_num-min_num+1) + min_num;


then do your comparisons against r1 and r2

You were defining the range (1) and the random number (always 0) well in advance of the user input. Btw, are you planning to stop that infinite while loop somehow?
Last edited on
@icy1
Thanks for the replies !

but if i dont define

int min_num=0, max_num=0;

the error is uninitialized local variable used

Oh yea im plannig to exit the programme.
Solved it. THANK YOU !
but if i dont define

int min_num=0, max_num=0;

the error is uninitialized local variable used

They're defined in your original line 32, from the user input, as you prob figured out, which happens before their usage in my suggestion (~line 43). Glad it works.
Here you have taken the wrong value which does not match according to your deserve value If you have found true result according to given input then you need to change in this int values as well as if conditional loop.
Here I have define the mistake which is puting in this code.
int range = (max_num - min_num) + 1;
int rand_gen = rand() % range + min_num;
if (rand_gen % 2 == 0 && rand_gen > 0)

If you want to gain more deep knowledge about coding then visit at http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html
Topic archived. No new replies allowed.