Random number generator with triangular distribution

Registered by Michael-Olaf

Implement a random number generator with triangular probability dristribution. Input parameters are: minimum, most likely, maximum

Blueprint information

Status:
Complete
Approver:
Michael-Olaf
Priority:
Essential
Drafter:
Michael-Olaf
Direction:
Approved
Assignee:
Michael-Olaf
Definition:
Approved
Series goal:
Accepted for trunk
Implementation:
Implemented
Milestone target:
milestone icon 0.3
Started by
Michael-Olaf
Completed by
Michael-Olaf

Whiteboard

Following the code for such a random generator:

private static function triangular($min,$most,$max){
     $u=rand()/getrandmax();

     if ($u <= ($most-$min)/($max-$min)){
       $r=$min+sqrt($u*($max-$min)*($most-$min));
     } else {
       $r=$max-sqrt((1-$u)*($max-$min)*($max-$most));
     }

     return $r;
}

Alternative algorithm 1 (does not work if the most likely value is not in the middle):

private static function triangular($p,$m,$o){
  $pmin = $p*2 - $m;
  $omax = $o*2 - $m;
  $y = 0;
  if($pmin < 0){
   $y = $pmin * -1;
  }
  $rp = (rand()/getrandmax()*(($m+$y)-($pmin+$y)))+($pmin+$y);
  $ro = (rand()/getrandmax()*(($omax+$y)-($m+$y)))+($m+$y);
  $result = ($rp + $ro)/2;
  $result -= $y;
  return $result;
}

Alternative algorithm 2:

private static function triangular($p,$m,$o){
   $side = rand()/getrandmax();
   $border = ($m-$p)/($o-$p);
   if($side < $border){
     $r1 = (rand()/getrandmax()*($m-$p))+$p;
     $r2 = (rand()/getrandmax()*($m-$p))+$p;
     $result = max($r1,$r2);
   }else{
     $r1 = (rand()/getrandmax()*($o-$m))+$m;
     $r2 = (rand()/getrandmax()*($o-$m))+$m;
     $result = min($r1,$r2);
   }

   return $result;
}

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.