Hello:
I was looking for shuffling algorythms and found some in the PAQ. I also found something called the Fisher-Yates Shuffle in some perl docs. Problem is that, more than implementations, I would like explanations on how they work. You see, I know they work, I can see them work, but I seem to be pretty dense in these matters and get lost when someone starts talking about permutations and n!..... could someone please explain in details the inner workings and theory behind a simple shuffle like:
/* Taken from from Ozo's answer, PAQ Q.10060202 */
for( i=1; i<sizeof(array)/sizeof(array[0]); i+= 1 ){
int r = random()%(i+1);
int temp = array[i]; array[i] = array[r]; array[r] = temp;
}
I'm not in a position to comment on the quality of the algorithm, other than that my experience is that ozo is very good at this stuff. What the loop does is:
The assumption is that the elements of array contain "representations" of the cards in the deck (or whatever you are shuffling.
for( i=1; i<sizeof(array)/sizeof(array[0]); i+= 1 ){
// sizeof(array) will result in the total number of bytes in the array
// sizeof(array[0]) will result in the number of bytes in the first element
// thus sizeof(array)/sizeof(array[0]) is equal to the number of elements in the array
// i.e. it is a general way of setting the number of iterations
int r = random()%(i+1);
//random() (or the more standard rand() return
// a pseudo-random number in the range 0 - RAND_MAX
// by modding it with i+1 you will get a number from 0 to i
int temp = array[i]; array[i] = array[r]; array[r] = temp;
// shuffle: take the "next" "card" (in array[i])
// and swap it with the card in array[r]
WOW! Thanx...... I completely understand the logic behind it now! :)
Now I see why Ozo and the others talk about factorials and permutations... I get it.... thank you so much. :)
-dZ.
0
If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.
oh... and I always like to give credit where credit is due so... thanx to ozo for the algorythm, the comments in the other answers, and for just being such a cool guy and helping us all when we need it :)
Another way to think of is that at each step, you start with an array with elements 0..i-1 fully shuffled, and produce from that an array with elements 0..i fully shuffled.
Btw.... ozo and imladris,
someone just pointed out to me something and since I don't really know all that about mathematical probabilities and stuff, i don't know what to make of it:
Wouldn't the first element of the array have more chances of being "shuffled" than the last element? I mean, since everytime that you iterate thru the loop you pick a number from 0 to i, if you have an array of lenght n, the nth element will only be shuffled once (swapped on the last iteration) while the first element (0) has the probability of being swapped around once per every element on the list, since it is within the range of the random number being picked at every iteration. You know what I'm saying? does this make sense?
I understand that if your rand() function picks element (lets say) 0 every time then its not random at all, but if we consider a "perfectly random" number generator, and all numbers have absolutely the same chance to be picked, then that 0th element has good chance of being picked every time, right?
Btw.... ozo and imladris,
someone just pointed out to me something and since I don't really know all that about mathematical probabilities and stuff, i don't know what to make of it:
Wouldn't the first element of the array have more chances of being "shuffled" than the last element? I mean, since everytime that you iterate thru the loop you pick a number from 0 to i, if you have an array of lenght n, the nth element will only be shuffled once (swapped on the last iteration) while the first element (0) has the probability of being swapped around once per every element on the list, since it is within the range of the random number being picked at every iteration. You know what I'm saying? does this make sense?
I understand that if your rand() function picks element (lets say) 0 every time then its not random at all, but if we consider a "perfectly random" number generator, and all numbers have absolutely the same chance to be picked, then that 0th element has good chance of being picked every time, right?
Or am I just getting even more confused?
TIA,
-dZ.
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.
The assumption is that the elements of array contain "representations" of the cards in the deck (or whatever you are shuffling.
for( i=1; i<sizeof(array)/sizeof(arr
// sizeof(array) will result in the total number of bytes in the array
// sizeof(array[0]) will result in the number of bytes in the first element
// thus sizeof(array)/sizeof(array
// i.e. it is a general way of setting the number of iterations
int r = random()%(i+1);
//random() (or the more standard rand() return
// a pseudo-random number in the range 0 - RAND_MAX
// by modding it with i+1 you will get a number from 0 to i
int temp = array[i]; array[i] = array[r]; array[r] = temp;
// shuffle: take the "next" "card" (in array[i])
// and swap it with the card in array[r]
}