Question

Hello, I am working on a C++ pick 5 lottery game that gives you the option...

Hello,
I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every time the program runs.
I would really appreciate any help I could get on this. Thank you

Here is a copy of my code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int list_size =5; //declare variables
int seqSearch(const int list[],int listLength,int searchItem);

void generatePick5Numbers(int list[], int length);
int main()
{
int winningNumbers[list_size];
int playerPick[list_size];
int numbersMatched = 0;

srand(time(0));
generatePick5Numbers(winningNumbers, list_size);
   char ch;
do{
   cout << "Welcome to Pick 5 Lotto!" << endl;
   cout << "Enter 5 numbers between 1 - 9, inclusive." << endl;
   cout << "Include a space between each number: "<<endl;

   for (int i = 0; i < list_size; i++) //store random number in array
   cin >>playerPick[i];
   cout << endl;

   for (int i = 0; i < list_size;i++)
   if (winningNumbers[i] == playerPick[i])//compare players number, check to see if they are equal to random generated number
   {

     
   numbersMatched++;
   }

   cout << endl;

   if (numbersMatched == 5)
   cout << "YOU WIN!!!" << endl;
   else
   {
   cout << "You matched " << numbersMatched << " numbers in the correct position." << endl;
   cout << "The winning numbers are:"<< endl;
   for (int i = 0; i < list_size; i++)
   cout <<winningNumbers[i] << endl;

   cout << "Feeling Lucky? Try Pick 5 Lotto Again!" << endl;
   cout << endl;
   cout <<"Enter 'Y/y' to play again." << endl;
   cin>> ch;

   }
   }while(ch == 'y'|| ch=='Y');

return 0;
}

void generatePick5Numbers(int list[], int length)
{
int num;
int count;

//srand to generate random numbers

list[0] = static_cast<int> (rand()) % 9 + 1;
count = 1; //store random number 1-9 in an array

while (count < 5)
{
num = static_cast<int> (rand()) % 9+1;
if (seqSearch(list, count, num) == -1)
{
list[count] = num;
count++;
}
}
}

int seqSearch(const int list[], int listLength, int searchItem)
{
int loc;
bool found = false;

loc = 0;

while (loc < listLength && !found)
if (list[loc] == searchItem)
found = true;
else
loc++;

if (found)
return loc;
else
return -1;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int list_size =5; //declare variables
int seqSearch(const int list[],int listLength,int searchItem);

void generatePick5Numbers(int list[], int length);
int main()
{
int winningNumbers[list_size];
int playerPick[list_size];
int numbersMatched = 0;

srand(time(0));
char ch;
do{
generatePick5Numbers(winningNumbers, list_size);
cout << "Welcome to Pick 5 Lotto!" << endl;
cout << "Enter 5 numbers between 1 - 9, inclusive." << endl;
cout << "Include a space between each number: "<<endl;

for (int i = 0; i < list_size; i++) //store random number in array
cin >>playerPick[i];
cout << endl;

for (int i = 0; i < list_size;i++)
if (winningNumbers[i] == playerPick[i])//compare players number, check to see if they are equal to random generated number
{


numbersMatched++;
}

cout << endl;

if (numbersMatched == 5)
cout << "YOU WIN!!!" << endl;
else
{
cout << "You matched " << numbersMatched << " numbers in the correct position." << endl;
cout << "The winning numbers are:"<< endl;
for (int i = 0; i < list_size; i++)
cout <<winningNumbers[i] << endl;

cout << "Feeling Lucky? Try Pick 5 Lotto Again!" << endl;
cout << endl;
cout <<"Enter 'Y/y' to play again." << endl;
cin>> ch;

}
}while(ch == 'y'|| ch=='Y');

return 0;
}

void generatePick5Numbers(int list[], int length)
{
int num;
int count;

//srand to generate random numbers

list[0] = static_cast<int> (rand()) % 9 + 1;
count = 1; //store random number 1-9 in an array
srand(time(0));
while (count < 5)
{
num = (rand() % 9)+1;
if (seqSearch(list, count, num) == -1)
{
list[count] = num;
count++;
}
}
}

int seqSearch(const int list[], int listLength, int searchItem)
{
int loc;
bool found = false;

loc = 0;

while (loc < listLength && !found)
if (list[loc] == searchItem)
found = true;
else
loc++;

if (found)
return loc;
else
return -1;

}

Add a comment
Know the answer?
Add Answer to:
Hello, I am working on a C++ pick 5 lottery game that gives you the option...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or...

    Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or answer the question N 1. In s tructures, the computer repeats particular statements a certain number of times depending on some condition(s) a. looping C. selection b. branching d. sequence 2. What is the output of the following CH code? count = 1; num - 25; while (count < 25) numnum - 1: count++; cout << count << " " << num << endl;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT