Title: Random Output "apple", "orange", "banana" demand output "apple" probability is 50%, "orange" probability is 30%, "banana" probability is 20%
Analysis
Programme one:
The most commonly used rand (1,10) to handle output from apples 6 to 8 oranges 9 to 10 bananas if it is 5 or less output
<?PHP$arr=Array(' Apples ', ' oranges ', ' bananas ');$i=Mt_rand(1,10);if($i<=5){Echo $arr[0];}Else if($i>=9){Echo $arr[1];}Else{Echo $arr[2];}
It's not scientific, however, because Rand is so unreliable.
Scenario Two:
Array_rand to handle it.
<? PHP $arr Array (' Apples ', ' apples ', ' apples ', ' apples ', ' apples ', ' oranges ', ' oranges ', ' oranges ', ' bananas ', ' bananas '); $key=array_rand($arr); $arr [$key];? >
Is this really true? Not flattering.
Programme III:
Str_shuffle to handle it.
<? PHP $array=array(' 1 ' = ' apple ', ' 2 ' = ' orange ', ' 3 ' = ' banana '); $string= "1111122233"; $key=str_shuffle($string); Echo $array [$key{0}];? >
Looks like I think it's the most reliable.
Interview questions PHP random get probability results