views:

28

answers:

2

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,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

A: 

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

Sarfraz
but i want it to choose zero more often then than the other numbers, so i thought let me give it a 50% chance, is it not allowed, or its bad practice, im nort sure
getaway
@getaway: Giving 50% to some number in random process makes it partial random and sometimes this is needed according to requirements of your application. Even in some games this is common.
Sarfraz
@sarfraz , thank you yeh this what my application requires, i want it to be partially random, that was the word im looking for, your a genius
getaway
+1  A: 
function getPair()
{
    $colors = array( "blue", "yellow" );
    $elem = $colors[ rand( 0, count( $colors ) - 1 ) ];
    return array( rand( 0, 1 ) * rand( 1, 5 ), $elem );
}
Christopher W. Allen-Poole
cheers thats works perfectly, thanks
getaway