Forum Moderators: coopster

Message Too Old, No Replies

question about array_rand()

are values always unique?

         

ln_tora

7:40 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Just a quick question abut the array_rand function. I'm trying to get 3 unique, random values from an array, and was wondering if array_rand does just that, or is there a chance of a duplicate value, as with using rand().

coopster

7:51 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about using shuffle() [php.net] and then grab the first three indexes out of the array? At least you'll know they are all random and not repeats.

ln_tora

7:55 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Hadn't gotten to shuffle yet. Certainly doable that way, though I'm still curious about array_rand() returning duplicates. (One of those things I should know for later.)

coopster

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

WebmasterWorld Administrator 10+ Year Member



Maybe I misunderstood you...are you talking about passing array_rand() the optional second argument and specifying how many entries you want to pick in one shot? Then you should be fine. To the best of my knowledge, it returns unique key values.

ln_tora

10:46 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Just wanted to be sure. Thank you!