PHP actions in ratio -


i have 3 options, , have happen in particular ratio. out of 10 times, ratio:

  • 8x option 1
  • 1x option 2
  • 1x option 3

right have following:

if (rand(8,10)) {     option1(); } elseif (rand(1,10)) {     option2(); } elseif (rand(1,10)) {     option3(); }  

but gets option 1 way more 8 times. option 2 never occurs , option 3 rarely.

so, correct way achieve this?

try this.

$roll = rand(1,10);    if ($roll == 1) {     option2(); } elseif ($roll == 2) {     option3(); } else {     option1(); }  

if random number 1 (10% of time), option 2.

if random number 2 (10% of time), option 3.

if random number else (80% of time), option 1.


Comments