random number selection

This is a discussion on random number selection within the C++ Programming forums, part of the General Programming Boards category; I'm currently trying to write a programme that will randomly select 6 numbers between 1 and 49 for the lottery ...

  1. #1
    Unregistered
    Guest

    Unhappy random number selection

    I'm currently trying to write a programme that will randomly select 6 numbers between 1 and 49 for the lottery but don't know where to start, can anyone give me some ideas or help on what to do?
    Thanks, Mike..........

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    hmm
    if you don't want the possibility of having 2 of the same number, i'd setup 2 arrays. One array is for the lotto numbers that are picked and the other contains all the possible lotto numbers.
    it'd look something like this

    const int MAXPICKED = 6;
    const int MAXLOTTONUM = 49;
    const int USEDNUMBER = 0;

    int lottopot[MAXLOTTONUM];
    int lottowin[MAXPICKED];
    int x; //counter
    int randnum; //will hold random number

    //initialize the lotto number pot
    for(x = 0 ; x < MAXLOTTONUM; x++)
    lottopot[x] = x+1;

    //pick numbers
    for(x = 0; x < MAXPICKED ; x++)
    {

    do
    {
    randnum = /*get random number here*/;
    }
    while(lottopot[randnum] != USEDNUMBER);
    lottowin[x] = lottopot[randnum]
    lottopot[randnum] = USEDNUMBER;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    also note that the lotto numbers in lottopot would be from 1-49, but the random number would need to be 0-48

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 01:46 AM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21