user1322720
user1322720

Reputation:

Conditional probability in PHP

I want to draw random numbers between 1 and 100 in PHP. I want even numbers to be drawn with a probability of 20%, odd numbers with a probability of 80%.

How can I implement this?

Upvotes: 1

Views: 379

Answers (1)

miorel
miorel

Reputation: 1833

function wtf_random() {
  $ret = 2 * rand(1, 50);
  if (rand(1, 5) !== 1) {
    --$ret;  
  }
  return $ret;
}

Upvotes: 6

Related Questions