开发者

dealing with random numbers in php?

i want a little php function to choose a number between 0 to 5, with 50% percent chance that it will be zero, and also choose between two strings at the same time randomly:

the algorithm:

choose number between 0,1,2开发者_如何学JAVA,3,4,5 randomly
(50% chance for zero, 50% chance for 1,2,3,4,5)
and 

choose string blue, yellow randomly

return(number, string); 

can php do that in one function. cheers :)) thanks


function getPair()
{
    $colors = array( "blue", "yellow" );
    $elem = $colors[ rand( 0, count( $colors ) - 1 ) ];
    return array( rand( 0, 1 ) * rand( 1, 5 ), $elem );
}


50% chances for the 0 makes random process biased. See this:

  • Generating Biased Random Numbers
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜