Forum Moderators: coopster
Try this out for yourself. It rarely changes.
<?php
$pictures[] = "one";
$pictures[] = "two";
$pictures[] = "three";
$pictures[] = "four";
$pictures[] = "five";
$pictures[] = "six";
$pictures[] = "seven";
$pictures[] = "eight";
$pictures[] = "nine";
$pictures[] = "ten";
srand ((float)microtime()*1000000);
shuffle($pictures);
for ( $i = 0; $i < 4; $i++ )
{
echo $pictures[$i];
print "<br>";
}
?>
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() [php.net] or mt_srand() [php.net] as this is now done automatically.
Why is shuffle so crap and is there and alternative?
Certainly not a crap!
Imagine, in your example you have ten pictures. What do you think are the chances of say picture ‘six’ to be in the first four slots? That’s 4 out of 10. Right? Now, 40-percent! It’s a very high probability mark. Clearly, its very likely that picture ‘six’, in several *draw*, can still capture any of the first four slots.
In other words, you may see that it seldom change for the reason that you have a small number of items in your set in addition to a high probability rate.
The C source is hard to decipher (for me), but it seems like it walks through the array and swaps the current elements with another randomly chosen element. I don't see any obvious problem with this method, but perhaps a mathhead would be able to explain why it's not producing uniformly shuffled results. Any geniuses in the house?
IDG offers an alternative to shuffle() called create_randomized_array(). It works by creating a temporary array, the same length as the original set, filled with random numbers. Then the temp array is sorted by value, thus mixing up the order of the keys. Then we walk through the temp array, use the temp key to grab elements from the original set and push them onto a new array, which is then returned.
I won't paste the function here for copyright reasons, though if you have the book or can borrow a copy of it, it's on page 230. Their function performs noticeably better.
There are some other replacements offered in the comments on this page [php.net]
Cheers,
httpwebwitch
[edited by: jatar_k at 9:10 pm (utc) on July 14, 2004]
[edit reason] swapped to php.net url [/edit]
In other words, you may see that it seldom change for the reason that you have a small number of items in your set in addition to a high probability rate.
johnerazo, your reply is a bit misleading. The probability of repeated results is not constant, it actually decreases with each shuffle.
If you flip a coin, the probability of "heads" is 50%. Then, what is the probability of flipping "heads" again? It's not 50% any more. If that were so, I'd have a 50% chance of flipping "heads" 100 times in a row, when in reality it's 1 in 9.33E+157.
The number of permutations of a set of length n is n! (factorial). With 10 elements, the number of permutations is 3628800.
The chances of any one element ending up in the same position after the shuffle is indeed 1/10.
However for two elements to remain unchanged, it's not 20%.
The probability of TWO elements being in the same place after the shuffle is 1/10 * 1/9, or 0.011..., or approximately 1%.
The probability of THREE elements remaining in their original positions is 1/10 * 1/9 * 1/8, or 0.00138..., or approximately 0.13%.
That series continues until you get the probability of returning an unshuffled array, which is 1/n!
If you're looking at the first 4 numbers as a test of randomness, the probability of the 4 unchanged numbers being in the same first four positions is 1/50400000, or ~1.98E-08, ... which is very unlikely.
So considering the coin-flipping example, you will get decreasing probability over time that *any* pattern will re-occur, and it is almost unfathomable that *any* 4 numbers would reoccur, in order, in positions 1,2,3,4, twice consecutively. Unless this is one of those days when betting on 1:50400000 odds pay off. :)
If you flip a coin, the probability of "heads" is 50%. Then, what is the probability of flipping "heads" again? It's not 50% any more.
Every statistics class I've ever had refutes this statement. Each flip of the coin is statistically independent from the last flip, so the probability of flipping "heads" each time is still 50%.
However for two elements to remain unchanged, it's not 20%.
httpwebwitch, I'm not trying to be a math guru here but what I am saying is that the chances of 1 picture to be chosen is 1/10. I'm not speaking about 2 pictures, or 3, and certainly not in the same position. Now, in a situation given by ukgimp, a single picture is given 4 possible position in the set, to be exact the initial four positions. I believe that there is a very high posibility that this ONE and the SAME picture may take ANY of the first four position in a single shuffle. Chances are (1/10) + (1/9) + (1/8) + (1/7) or 0.478968 or 47.8968%.
I'd be very glad if somebody would correct me with my math, to be honest I'm not good at this.
Every statistics class I've ever had refutes this statement
Then perhaps your statistics teachers were misleading you. Individual flips can be independently measured, at 50%. The phenomenon I was describing is a consecutive group of flips. Different. As an analogue to an array before and after shuffling. 'Nuff said.