1. 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. 2. Originally Posted by laserlight 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. Originally Posted by wolly 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. 4. Thank you for all your help but I expected a program like this:
nk — ImgBB
This example shows that if you input 6 numbers the program shows you the chances in percentage of winning or losing.
Except the matching 6 numbers from 1 number which is useless. 5. Right, so
Code:
```int guess;
for ( i = 0 ; i < 6 ; i++ ) {
printf("Enter a number ");
scanf("%d",&guess[i]);
}``` 6. Just wanted to point out another simple approach to the whole random-shuffle thing. Use qsort() with some kind of randomizing callback. Crude example:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int randomize(const void* ignore, const void* unused)
{
return rand() % 2 ? 1 : -1;
}

int main(void)
{
srand(time(NULL));
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const int length = sizeof(array) / sizeof(int);
qsort(array, length, sizeof(int), randomize);
for(int index = 0; index < length; ++index)
printf("%d\n", array[index]);
}``` 7. Originally Posted by Sir Galahad
Use qsort() with some kind of randomizing callback.
That's probably not a good idea though as it technically goes against the C standard requirements for qsort:
When the same objects (consisting of size bytes, irrespective of their current positions in the array) are passed more than once to the comparison function, the results shall be consistent with one another. That is, for qsort they shall define a total ordering on the array, and for bsearch the same object shall always compare the same way with the key.
and then there's the thing whereby a general purpose comparison-sort algorithm as would be used to implement qsort takes O(n log n) in the average case (and possibly worst case, but not always), whereas a Knuth/Fisher-Yates shuffle takes linear time even in the worst case, and faster yet (for the constant factor) with a partial shuffle. 8. Originally Posted by laserlight That's probably not a good idea though as it technically goes against the C standard requirements for qsort:

and then there's the thing whereby a general purpose comparison-sort algorithm as would be used to implement qsort takes O(n log n) in the average case (and possibly worst case, but not always), whereas a Knuth/Fisher-Yates shuffle takes linear time even in the worst case, and faster yet (for the constant factor) with a partial shuffle.
I see! After some testing it does indeed seem to lead to something like N*log(N) comparisons. I don't think I've ever heard of the Knuth-Fisher-Yates shuffle. Such an elegant and efficient approach, yet so simple... 9. Popular pages Recent additions code, create, generate, numbers, program 