php - Randomly executing a branch of code one thirds of the time -


i have question. have following code :

protected function definir( $ticketinfos ) {     if($ticketinfos->ainfosticket["probabilitegain"]>0)     {         $ticketinfos->setgainrecup( $this->getgain() );     }     else     {         $ticketinfos->setgainrecup( 0.0 );     }  } 

probabilitegain constant witch equal 100. want make winning match 1/3. 2 times want : $ticketinfos->setgainrecup( $this->getgain() ) , 1 time : $ticketinfos->setgainrecup( 0.0 ); best solution ? thx in advance

i approach using php rand() function.

something this:

protected function definir( $ticketinfos ) {     $randomnumber = rand(1, 3);     if($randomnumber < 3)     {         $ticketinfos->setgainrecup( $this->getgain() );     }     else     {         $ticketinfos->setgainrecup( 0.0 );     }  } 

if $randomnumber 1 or 2, execute $this->getgain(); if $randomnumber 3, execute other function.

you can find more information php rand function here


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -