Thread: MySQL + Arrays?
View Single Post
  #4  
Old 09-29-2011, 06:06 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 574,601
Hall of Famer is on a distinguished road
Default

Well the problem is that you are choosing two random values from the array input[], while the function array_rand() automatically sort the two generated values. The possible combinations from the array $rand_keys[] is:

PHP Code:
$rand_keys[0] = 3$rand_keys[1] = 4
$rand_keys
[0] = 3$rand_keys[1] = 5
$rand_keys
[0] = 3$rand_keys[1] = 6
$rand_keys
[0] = 4$rand_keys[1] = 5
$rand_keys
[0] = 4$rand_keys[1] = 6
$rand_keys
[0] = 5$rand_keys[1] = 
As you can see from the above 6 combinations, the first element $rand_keys[0] never contains 6, while the second element $rand_keys[1] never contains 3. If you print the first element to the screen, it has 1/2 chance to be 3, 1/3 chance to be 4, and 1/6 chance to be 5. This is why 5 appears in a much smaller chance than 3 and 4, while 6 never shows up. Hope this explanation is clear enough, I personally would not use the function array_rand() to generate more than one random numbers.

There are many ways to resolve this glitch. If you insist on working with array_rand() function, Id recommend you use PHP shuffle which destroys the orders in an array and randomize it. The instruction on how to use shuffle can be found from php's official site:
http://php.net/manual/en/function.shuffle.php
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote