generate lottery number(S) between 25-75

Trying to generate series of 10 random numbers between 25-75 for like lottery ticket. allowing the computer to pick random numbers every time...
I know i'm missing something like a for loop even though i have the do-while loop set up perfectly fine. its just the expression and condition that i am having trouble with.

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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    

const int upBoundary=75;
const int lowBoundary=25;
const int numSeries=10;
int num, randNum;
string runAgain;
        
srand(static_cast<unsigned int>(time(0)));		// seed random number generator
        
cout << "Welcome to Guess My Number " << endl;
cout << "Computer it's time for you to give me some random numbers " << endl;
    
do
{
    if(randNum>=lowBoundary||randNum<=upBoundary||randNum++)
    {
    randNum=(lowBoundary+(rand()% (upBoundary-lowBoundary+1)));		// pick random numbers between max and min
        cout<< "Would you like to try this again? (Yes or No)" <<endl;
        cin>> runAgain;
    }
    else
    {
     cout << " Please try again pick a number between" << lowBoundary << " - " << upBoundary << endl;
    }
     
     randNum=(lowBoundary+(rand()% (upBoundary-lowBoundary+1)));		// pick random numbers between max and min
     cout<< "Would you like to try this again? (Yes or No)" <<endl;
     cin>> runAgain;
     }
while(runAgain=="Yes"|| runAgain=="yes"|| runAgain=="y");
    
    

return randNum;
    
}
Line 25; First time through the loop, randNum is uninitialized. You're testing garbage.
What's the point of incrementing randNum in your condition? You're incrementing garbage.

line 44: main should return 0, not randNum.

I don't see anything in your code that has to do with 10 random numbers.
Last edited on
what example would you use to generate random numbers... maybe i can get a better idea from your example.

numSeries=10 should be 10 random numbers within 25-75, but again still haven't quite figure out who to test it...
Why not use the standard random library?
http://www.cplusplus.com/reference/random/

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <random>

int main()
{
    std::minstd_rand gen;
    std::uniform_int_distribution<int> dist(25, 75);
    
    for(int i{}; i < 100; i++)
        std::cout << dist(gen) << "\n";
}
@integralfx I am not understanding half of that.... I am a beginner and still taking C++ course... i was told to use
to require at least 3 symbolic constants for
max=75
min=25
number of random numbers= 10

using:
lower boundary + ( rand() % (upper boundary – lower boundary + 1))
and
srand(0) however I'm using Xcode so i have to use:
srand(static_cast<unsigned int>(time(0)))
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
int main()
{
	srand(static_cast<unsigned int>(time(0)));

	const int upBoundary = 75, lowBoundary = 25, numSeries = 10;
	// Unused variable?
	//int num;
	int randNum = rand() % 75 + 1;	// Initialise randNum
	string runAgain;
        
	cout << "Welcome to Guess My Number " << endl;
	cout << "Computer it's time for you to give me some random numbers " << endl;

	/*
	How does the user input their number?
	*/
    
	do
	{
		/* 
		|| (logical or) means that if any of the conditions evaluate to true,
		the body of the statement will execute.

		Since you haven't initialised randNum, who knows when this statement
		will execute.
		*/
    	if(randNum >= lowBoundary || randNum <= upBoundary /*|| randNum++*/)
    	{
    		/* 
    		Please don't use extra parentheses for no reason. It makes the
    		code hard to read.

    		This is the order the expression below will be evaluated in:
    		1. (upBoundary - lowBoundary + 1)
    		2. rand() % above expression
    		3. lowBoundary + above expression
    		*/
    		randNum = lowBoundary + rand() % (upBoundary - lowBoundary + 1);
        	cout << "Would you like to try this again? (Yes or No)" <<endl;
        	cin >> runAgain;
    	}
    	else
    		cout << "Please try again pick a number between " << lowBoundary << " - " << upBoundary << endl;
     
        randNum = lowBoundary + rand() % (upBoundary - lowBoundary + 1);
        cout << "Would you like to try this again? (Yes or No)" << endl;
        cin >> runAgain;

	} while(runAgain == "Yes"|| runAgain == "yes" || runAgain == "y");

	return 0;	// returning 0 indicates success
}


Maybe you could have a function that generates 10 pseudo-random numbers and stores them in an array.
Last edited on
@integralfx

for the comments on them... it helps me better to understand the structure and what needed it to be done... however the user input, doesn't means user guessing the number correct? i want to have the computer pick 10 numbers for me.....

for the rest i gotta write the statement and expressions now...
Your OP doesn't say anything about the user guessing the random numbers. It only says to generate 10.
1
2
3
4
5
  cout << "Your 10 lottery numbers are: " << endl;
  for (int i=0; i<numSeries; i++)
  {   randNum = lowBoundary + rand() % (upBoundary - lowBoundary + 1);
       cout << randNum << endl;
  }


edit: corrected line 3 to replace << with =.
Last edited on
EVERYTHING WORKS!! but I forgot to mention in the beginning how to group the random numbers into

how many are in 65-75
how many are in 55-64
how many are in 45-54
how many are in 35-44
how many are in 25-34

do I have to use cascading decision, if so how?
You could do it with a series of if statements, but a simpler way is to use division.
1
2
3
4
5
  int arr[5] = {0};  // five buckets initialized to 0
  int n;                   // bucket number
// ...
  n = (randNum - lowBoundary) / 10;
  arr[n]++;


@AbstractionAnon

I haven't learned the array yet.... I'm familiar with if statements, if else statements, switch statement...


if (randNum<75||randNum>=65)
{
cout<< "there are " << randNum << " in 65-75 range " << endl;
}
if(randNum<64||randNum>=55)
{
cout<< "there are " << randNum << " in 55-64 range" << endl;
}
if(randNum<54||randNum>=45)
{
cout<< "there are " << randNum << " in 45-54 range" << endl;
}
if(randNum<44||randNum>=35)
{
cout<< "there are " << randNum << " in 35-44 range" << endl;
}
if(randNum<34||randNum>=25)
{
cout<< "there are " << randNum << " in 25-34" << endl;
}
else
{
cout<< "I'm assuming this is all the numbers that have been checked out so far ";
}
Your conditions should be &&, not ||.
Also check your conditions (<, >=) because they to not match the ranges you stated.
e.g. The first if will NOT include 75.

You're trying to do two things at one time that need to be done separately.
You're going to need to count how many are in each range as you generate them.
Then at the end you need to report the total of how many were in rach range.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.