Thread: Lotto 6/49 program

  1. #31
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    One issue with the suggested solutions is that the results will be slightly biased due to rand%(x), where x ranges from 49 to 44. For example, if using a 32 bit random number generator modulo 49, (2^32)%49 = 39, so values 1 to 39 have 87652394 chances of being picked while values 40 to 49 have 87652393 chances of being picked. A random number generator with more bits will reduce the small relative amount of bias. With a 64 bit random number generator, (2^64)%49 = 2, so values 1 to 47 have 376464164769582687 chances of being picked while values 48 to 49 have 376464164769582686 chances of being picked.

    The ideal method would use a random number generator with equal probability for all 13983816 (= 2 · 2 · 2 · 3 · 7 · 7 · 11 · 23 · 47) possible cases of 49 things taken 6 at a time. For this particular case, the code could just use random()%13983816, to select a set of 6 numbers.

  2. #32
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rcgldr View Post
    One issue with the suggested solutions is that the results will be slightly biased due to rand%(x), where x ranges from 49 to 44. For example, if using a 32 bit random number generator modulo 49, (2^32)%49 = 39, so values 1 to 39 have 87652394 chances of being picked while values 40 to 49 have 87652393 chances of being picked.
    How did you got this values? Ok... usually rand() implement LCG, that is less random in the lower bits, but being agnostic about the algorithm, I don't see why a random 32 bits value can generate more or less probable values due to modulo n...

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flp1969
    I don't see why a random 32 bits value can generate more or less probable values due to modulo n
    Think about picking one of four restaurants to eat at for lunch using a single roll of a six-sided die: if you use the entire range of the die using modulo, then two of the restaurants will be twice as likely to be selected as the other two. For example, restaurant A will be selected if the die roll is 1 or 5, restaurant B will be selected if the die roll is 2 or 6, but restaurant C will only be selected if the die roll is 3, and restaurant D will only be selected if the die roll is 4. A simple solution to this is to discard the roll if it is 5 or 6, i.e., re-roll the die such that you only pick a random number within a range congruent to the number of options.

    That said, I'm not concerned with this slight bias at all when applied to wolly's problem: wolly needs to get going, not focus on this just yet, and fixing the range to eliminate the bias is not that difficult anyway.
    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

  4. #34
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by laserlight View Post
    Think about picking one of four restaurants to eat at for lunch using a single roll of a six-sided die: if you use the entire range of the die using modulo, then two of the restaurants will be twice as likely to be selected as the other two. For example, restaurant A will be selected if the die roll is 1 or 5, restaurant B will be selected if the die roll is 2 or 6, but restaurant C will only be selected if the die roll is 3, and restaurant D will only be selected if the die roll is 4. A simple solution to this is to discard the roll if it is 5 or 6, i.e., re-roll the die such that you only pick a random number within a range congruent to the number of options.
    Ahhhhh.... I keep forgeting about distribution. Thanks. Those values are very clear to me now!

  5. #35
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    Now,I have another issue:
    Code:
    int lotto[49] = {  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   };
    int i;
    for(i=0;i<50;i++){
    printf("%d",lotto[i]);
    }
    It prints a size of 49 numbers from 1 to 49 but the numbers are all joined together like in this example:
    12345678910111213...4546474849
    That is what the compiler is showing me and I don't know how to change that

  6. #36
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Code:
    for ( i = 0; i < sizeof lotto / sizeof lotto[0]; i++)
      printf( "%d ", lotto[i] );
    putchar('\n');

  7. #37
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("%d",lotto[i]);
    So change the format string

    printf("%d ",lotto[i]);
    or
    printf("%d, ",lotto[i]);
    or
    printf("%d\n",lotto[i]);
    or
    printf("= %d\n",lotto[i]);


    > for(i=0;i<50;i++)
    No, the array has 49 elements, so it's < 49 in the loop.
    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.

  8. #38
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    You need to put something between the numbers if you want something between the numbers. It's up to you whether that's a space, newline, comma, or something else. Just add the number separator to the format string.

    You also have another issue: you're accessing the 50th element in an array that has only 49 elements!

    Edit: Drats! Beat by flp1969 and Salem! (That's what I get for being distracted at work!)

  9. #39
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    Code:
    int main() {
    int lotto[49] = {  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   };
    int i;
    for(i=0;i<49;i++){
    printf(" \n %d",lotto[i]);
    }
    Now I solved my problem. Now that I know how to type 49 numbers in an array how do you use the random function so that when I input 6 numbers the compiler would tell me if the answer is correct or wrong. Do you use the if() and else() funtions if you want to create 2 decisions?
    I know that when you want to input something you use the scanf() function and in my case I want to write 6 numbers in the compiler.
    After that I want to check if the numbers I picked are the winning numbers or losing numbers.(I did write that I want to use the random function rand() % but I don't understand how that applies to what I want to do)

  10. #40
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well next, let's introduce you to the idea of a shuffle.

    Code:
    // pick two random lotto number positions
    int p1 = rand() % 49;
    int p2 = rand() % 49;
    // now swap them over
    int temp = lotto[p1];
    lotto[p1] = lotto[p2];
    lotto[p2] = temp;
    Do that once, then examine your array using the print loop you've just created.

    If you're feeling adventurous at this point, put the shuffle code into a loop that runs 25 times, and then print out the lotto array.
    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.

  11. #41
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    Quote Originally Posted by Salem View Post
    Well next, let's introduce you to the idea of a shuffle.

    Code:
    // pick two random lotto number positions
    int p1 = rand() % 49;
    int p2 = rand() % 49;
    // now swap them over
    int temp = lotto[p1];
    lotto[p1] = lotto[p2];
    lotto[p2] = temp;
    Do that once, then examine your array using the print loop you've just created.

    If you're feeling adventurous at this point, put the shuffle code into a loop that runs 25 times, and then print out the lotto array.
    Ok,but how is that going to help me input 6 numbers and check if the input I gave is right or wrong?
    I only know to create an array of 6 elements but I don't know how I could continue
    Code:
      int picklotto[6]

  12. #42
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First, you said
    > I want to create a program which generates 6 numbers from a range of 1 to 49.

    > Ok,but how is that going to help me input 6 numbers and check if the input I gave is right or wrong?
    Because we haven't yet completed the first overall step of drawing 6 random numbers.
    We're nearly there.

    Show me your shuffle code.
    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.

  13. #43
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    @Salem I tried solving the issue with the random function and I am uncertain of myself if my method was correct or wrong. I wrote the code but it's a different shuffle.
    Here is the code:
    Code:
    int p1 = rand() % 49;
    int p2 = rand() % 49;
    int temp = lotto[p1];
    lotto[p1] = lotto[p2];
    lotto[p2] = temp;
    for(p1=0;p1<49;p1++){
    lotto[p1] = rand() % 49;
    printf("\n lotto[%d]",lotto[p1]);
    }
    for(p2=0;p2<49;p2++){
    lotto[p2] = rand() % 49;
    printf("\n lotto[%d]",lotto[p2]);
    }

  14. #44
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You start with this.
    Code:
    int main() {
      int lotto[49] = {  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   };
      int i;
      for(i=0;i<49;i++){
        printf(" \n %d",lotto[i]);
      }
    }
    Then this.
    Code:
    int main() {
      int lotto[49] = {  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   };
      int i;
      for(i=0;i<49;i++){
        printf("%d,",lotto[i]);
      }
      printf("\n");
    
      {
        // pick two random lotto number positions
        int p1 = rand() % 49;
        int p2 = rand() % 49;
        // now swap them over
        int temp = lotto[p1];
        lotto[p1] = lotto[p2];
        lotto[p2] = temp;
      }
      for(i=0;i<49;i++){
        printf("%d,",lotto[i]);
      }
      printf("\n");
    }
    And finally, this.
    Then this.
    Code:
    int main() {
      int lotto[49] = {  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   };
      int i;
      for(i=0;i<49;i++){
        printf("%d,",lotto[i]);
      }
      printf("\n");
    
      srand(time(0));
      for ( i = 0 ; i < 25 ; i++ )
      {
        // pick two random lotto number positions
        int p1 = rand() % 49;
        int p2 = rand() % 49;
        // now swap them over
        int temp = lotto[p1];
        lotto[p1] = lotto[p2];
        lotto[p2] = temp;
      }
      for(i=0;i<49;i++){
        printf("%d,",lotto[i]);
      }
      printf("\n");
    }
    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.

  15. #45
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    Ok,but how is that suppose to help me if I want to pick 6 numbers and not 49?

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