Thread: Problem with lottery game

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Problem with lottery game

    Hello! I programmed a little game, which runs a while loop for a couple of seconds, that is constrantly generating random numbers and at the end, the program outputs the most commonly generated number. My problem is that everytime I run the program, the most common number is always 90.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    
    using namespace std;
    
    
    int main() {
    
    
        time_t end = time(NULL) + 5; // time
    
    
        int retVal = 0;
        int mcn = 0;
        int arr[90];
    
    
        for(int x = 0; x < 90; x++) {
            arr[x] = 0;
        }
    
    
        srand(time(NULL));
    
    
        while (time(NULL) < end) {
             int n = rand() % 90;
    
    
            cout << n << endl;
    
    
            for(int x = 0; x < 90; x++) {
                if(arr[x] == n)
                    arr[x]++;
            }
    
    
        }
    
    
        for(retVal = 0; retVal < 90; retVal++) {
            if(arr[retVal] > mcn)
                mcn = arr[retVal];
        }
    
    
    
    
        cout << "The most common number is: " << mcn<< endl;
    
    
        return 0;
    }
    Last edited by Andor Nemeth; 02-26-2016 at 08:37 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It appears as if you want to count the occurrence of each random number (n) by incrementing the n'th index of the array each time that number is found.

    If this is the case, your logic is incorrect. (I'm assuming this is indeed the case - if I'm wrong, you can disregard this post.)

    Code:
    for(int x = 0; x < 90; x++) {
        if(arr[x] == n)
            arr[x]++;
    }
    This goes through each element of the array, and if the value at that position is equal to the random number, you increment that value.

    What you would want instead is simply:

    Code:
    arr[n]++;
    There is a similar problem in your second "for()" loop, where you're saving the count of a particular n instead of the value n itself.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    Yes, that was the problem. Thank you!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note that rand() is very biased depending on the implementation, and so is "% 90".
    I suggest you use C++11's <random> header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my lottery game
    By Elina Sumin in forum C Programming
    Replies: 8
    Last Post: 11-27-2014, 03:57 PM
  2. Need help with my lottery game
    By Elina Sumin in forum C Programming
    Replies: 1
    Last Post: 11-27-2014, 03:10 PM
  3. Lottery game Issue in Code...whats wrong?
    By thebridge in forum C Programming
    Replies: 6
    Last Post: 10-07-2010, 08:16 PM
  4. Lottery game
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2008, 01:55 PM
  5. Lottery game
    By geetard in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2005, 12:50 AM

Tags for this Thread