Thread: Lotto 6/49 program

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,509
    Quote Originally Posted by wolly
    Ok,but how is that suppose to help me if I want to pick 6 numbers and not 49?
    The idea is that if you have a list of 49 numbers, to select the first 6 numbers at random without replacement, you do a partial shuffle such that the first 6 numbers out of the 49 are done with the shuffling (or you can completely shuffle all 49, that's fine too, just extra work). Then, you just need to select these first 6 numbers, ignoring the remaining 43, and you're done. Doing this right is slightly tricky though: Salem's post #44 demonstrates a somewhat different shuffling approach that arguably does too much and yet risks doing too little: you need to shuffle the entire list, and yet if you don't have enough iterations, you won't be shuffling enough.

    A correct and efficient approach is as given in the Knuth/Fisher-Yates shuffle: you repeatedly randomly select an item from the "unshuffled" portion of the list and swap it into position; the item that was swapped out of position now forms part of the "unshuffled" portion of the list, whereas the item that was swapped into position joins the "shuffled" portion of the list. This is basically akin to having a bag of items and taking them one by one at random to join your sequential hand of items. Therefore, you can do a partial shuffle with just 6 iterations with one swap each and you're done, needing only 6 random numbers to be generated.

    You might find it easier to implement another approach: build your list of random numbers by generating random numbers in the range and checking to see if it already exists if your list of generated numbers. If it doesn't, you add it to the list, otherwise you regenerate the random number. This approach works fine here because the number of numbers you want is much smaller than the range.
    Last edited by laserlight; 1 Day Ago at 05:16 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    704
    Quote Originally Posted by laserlight View Post
    The idea is that if you have a list of 49 numbers, to select the first 6 numbers at random without replacement, you do a partial shuffle such that the first 6 numbers out of the 49 are done with the shuffling ....

    A correct and efficient approach is as given in the Knuth/Fisher-Yates shuffle: ...
    Incidentally, I mentioned using the Fisher-Yates algorithm in #4 and gave some code in #10 that does basically what you suggested. It seems like wolly either isn't reading or isn't understanding our replies.

  3. #48
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,680
    Quote Originally Posted by wolly View Post
    Ok,but how is that suppose to help me if I want to pick 6 numbers and not 49?
    Did it cross your mind that when you saw the output of
    Code:
      for(i=0;i<49;i++){
        printf("%d,",lotto[i]);
      }
    That changing it to
    Code:
      for(i=0;i<6;i++){
        printf("%d,",lotto[i]);
      }
    would just give you the 6 random numbers from the pool of 49 numbers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lotto program homework help
    By apelletier in forum C Programming
    Replies: 14
    Last Post: 03-11-2018, 02:09 PM
  2. Lotto program
    By MrQ in forum C Programming
    Replies: 3
    Last Post: 06-24-2013, 12:35 AM
  3. lotto program in c
    By vyshant in forum C Programming
    Replies: 7
    Last Post: 11-07-2011, 12:19 PM
  4. Lotto problem
    By Roaring_Tiger in forum C Programming
    Replies: 11
    Last Post: 03-13-2003, 10:17 PM
  5. Lotto game in C
    By fun2sas in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:19 PM

Tags for this Thread