Forum Moderators: coopster

Message Too Old, No Replies

Shuffle Command

... keeps shuffling it back into its original sequence!

         

internetheaven

1:02 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have this coding to jumble up numbers:

$a[example] = explode("+", $a[asdf]);
shuffle($a[example]);
$a[example2] = implode("+", $a[example]);
$a[example2] = str_replace("+", ' and ', $a[example2]);

But it seems as though 50% of the time the exploded sequence of numbers ends up in exactly the same sequence! Not much use. Is there any coding that will explode the sequence and shuffle it but ensure that it doesn't suffle it back into it's original sequence?

Longhaired Genius

1:25 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



If you have a small number of items the chances are quite high that after one shuffle they will end up in the same sequence. This is not a bug, it's just a characteristic of any shuffle, physical or virtual. Over several shuffles things will even out and every item will occupy every position a more or less equal number of time. If you can, don't worry about it, just relax and learn to love shuffle().

internetheaven

2:26 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you can, don't worry about it, just relax and learn to love shuffle().

If it wasn't a worry/problem then I wouldn't be asking about it. Thanks anyway for the sentiment, I take it that means you don't know of a way around it?

The problem is that I wanted to shuffle the strings so I used shuffle. It kind of defeats what I'm doing (sort of a lottery/raffle/gaming thing) if the numbers end up in the same order.

Longhaired Genius

2:44 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



Let me be more explicit. It's nothing to worry about. The apparent pattern is an illusion.

coopster

7:13 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That code works fine for me, as long as the value in the 'asdf' index of the $a array is a "+" separated string.
$a['asdf'] = 'one+two+three+four';
I ran a quick test and received random orders every time, even on these few numbers.

internetheaven

11:33 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let me be more explicit. It's nothing to worry about. The apparent pattern is an illusion.

I didn't find that any clearer. What I'm really interested in is actual, factual coding (if it exists) not a theological debate about the eternal nature and mythical buddhist perceptions of php ...

... how can the order re-arranging itself back into its original order be an "illusion"?

coopster - it does work for me too, but not every time. A large part of the time the string ends up right back where it started, which is not what I want. The strings vary from 3 numbers to 10 and the number of variables doesn't seem to make a difference.

Longhaired Genius

1:32 pm on Jan 9, 2005 (gmt 0)

10+ Year Member



Dear internetheaven, I'm only trying to help. Your code is ok. If you change it in the way you're contemplating you'll mess it up. Your problem stems from a misapprehension of probability. If you're planning to get into the lottery business maybe you should get a book on it.

mcibor

2:03 pm on Jan 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe a solution to your problem would be keeping shuffling, until the variable changes

do
{
$a[example] = explode("+", $a[asdf]);
$temp = $a[example];
shuffle($temp);
$a[example2] = implode("+", $temp);

}while($a[example] == $a[example2])

$a[example2] = str_replace("+", ' and ', $a[example2]);

You would certainly get it changed this time, as I think that's what you want.

PS. Remeber to put some limit into the loop as not to get caught in endless repeating.

grandpa

6:33 am on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Another possible solution?

Compare the shuffled results to a file or array of previous results.