...

View Full Version : Problem with shuffle() function



jeddi
01-25-2010, 12:13 PM
Hi,
I am using shuffle() to find a random item in an array.

The problem is that the array is generated from client entries and it
is possible that the entries can be the same value. In that case I can end up with an array like this:

Array ( [0] => EBFORTUNE [1] => EBFORTUNE [2] => EBFORTUNE [3] => EBFORTUNE )


In that case when the shuffle runs I get
a 500 Internal error

Is this normal?

How can I overcome it - or check that it is safe to run the
shuffle function ?

Thanks.

JAY6390
01-25-2010, 12:20 PM
The shuffle function wont be causing the 500 internal error. This is a php function so it would just spit out a PHP error if there was anything wrong. I'd suggest contacting your host as this is an APACHE issue

jeddi
01-25-2010, 12:23 PM
OK,

I have just realized why I am getting the error.

This is my code:


shuffle($cb_promo_hits);
$hit_1 = $cb_promo_hits[0];
$hit_2 = $cb_promo_hits[1];
while ( $hit_2 == $hit_1){
shuffle($cb_promo_hits);
$hit_2 = $cb_promo_hits[0];
}

It is because I want two results from the array and I want the
them to be different.

So when there is no different item
- I produce a never ending loop :eek:

Now my problem is - what can I do about it ?

In this situation I would accept the two
variables to be the same ( as there is no other choice).

But how can I test for this ?

Is there a way to test the array for
differing values ?

Any help appreciated, thanks

JAY6390
01-25-2010, 12:28 PM
Use this code

if(count(array_count_values($array_here)) > 1) {
// CODE HERE FOR MORE THAN ONE VALUE
}else{
// CODE HERE FOR ONE VALUE ONLY
}

abduraooft
01-25-2010, 12:28 PM
How about removing duplicate values using array_unique(), before calling shuffle? (You'd need to ensure that there's are least two entries, to proceed with rest of your code)

JAY6390
01-25-2010, 12:30 PM
The problem with array_unique is that it will remove those from the original array, and the OP may not want to

Fou-Lu
01-25-2010, 01:00 PM
The problem with array_unique is that it will remove those from the original array, and the OP may not want to

No it doesn't, array_unique returns an array with all elements minus the duplicates.



$unique = array_unique($cb_promo_hits);
shuffle($unique);
$hit_1 = $cb_promo_hits[0];
$hit_2 = $cb_promo_hits[1];



array_merge isn't necessary, I wasn't certain if shuffle reindexed, but it does.

kbluhm
01-25-2010, 01:01 PM
shuffle( $cb_promo_hits );

$hit_1 = array_shift( $cb_promo_hits );

do
{
$hit_2 = array_shift( $cb_promo_hits );
}
while ( ! empty( $cb_promo_hits ) && $hit_1 == $hit_2 );

It only needs to be shuffled once. If they are still the same value and there are no items left to pull from the array, the loop will cease.

JAY6390
01-25-2010, 02:11 PM
Oops, so it does. My apologies abduraooft

jeddi
01-26-2010, 06:02 AM
A Real big thanks for
all your imput.

I can not use the unique methods because I actually want
to allow the duplicates !

As I mentioned in my post: the array "is generated from client entries"
and the more that enter they get a higher chance of being selected.

( I mean if someone has four horses in the race they are more likely to have a winner)

I like this array_shift() function here: :)



shuffle( $cb_promo_hits );

$hit_1 = array_shift( $cb_promo_hits );

do
{
$hit_2 = array_shift( $cb_promo_hits );
}
while ( ! empty( $cb_promo_hits ) && $hit_1 == $hit_2 );


This looks fine, but does it solve my
problem about when all four entries in the
array are the same ?

I will test it but it looks to me like
it wont give me a $hit_2 in the situation
that causes my problem ... or does it ?


.
ADDED:

I guess that you assume that I use JAYs
suggestion first to do the checking ?
then change my shuffle to the above.


So I end up with:



if(count(array_count_values($cb_promo_hits)) > 1) {
shuffle( $cb_promo_hits );
$hit_1 = array_shift( $cb_promo_hits );

do {
$hit_2 = array_shift( $cb_promo_hits );
}
while ( ! empty( $cb_promo_hits ) && $hit_1 == $hit_2 );
}
else{
$hit_1 = $cb_promo_hits[0];
$hit_2 = $cb_promo_hits[1];
}




.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum