1. 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. Originally Posted by rcgldr
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. 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.

4. Originally Posted by laserlight
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. 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. Code:
```for ( i = 0; i < sizeof lotto / sizeof lotto[0]; i++)
printf( "%d ", lotto[i] );
putchar('\n');```

7. > 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.

8. 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. 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. 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.

11. Originally Posted by Salem
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. 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.

13. @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]);
}```

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");
}```

15. Ok,but how is that suppose to help me if I want to pick 6 numbers and not 49?