Jump to content

Probabilities


grilldor

Recommended Posts

Ive achieved this in a somewhat cheap way that is not usable for further complexification of my code. What i really am looking for is to generate a number between 0.000 to 1.000. Thats easy of course. Problem is... I need a better probability to land on 0.7-0.8. I thought of using some gaussian type function to achieve this, problem is i have no idea how to do it... The reason i need this method is that i want to be able to play with the horizontal movement of the bell so that probabilities could change to 0,8-0,9 for example. Any math guy here can give me a hand?
Link to comment
https://forums.phpfreaks.com/topic/26642-probabilities/
Share on other sites

for($i=0;$i<=1;$i++){
  $first = $i;
  for($j=0 ;$j<10;$j++){
    $second = $j;
    for($k=0;$k<10;$k++){
      $third = $k;
      for($g=0;$g<10;$g++){
        $arr[]=$first . "." . $second . $third . $g;
      }
    }
  }
}
for ($i=0;$i<100;$i++){
  $arr[]=0.7;
  $arr[]=0.8;
}
$cnt = count($arr);
$rnd=$arr[rand(0,$cnt)];
echo "Random:  " . $rnd;
Link to comment
https://forums.phpfreaks.com/topic/26642-probabilities/#findComment-121895
Share on other sites

Ooh--even better.

switch (rand(0,1)){
  case 0:
//    $tst = rand(0,1);
    if(rand(0,1)){//$tst){
      $rnd = "0." . rand(0,9) . rand(0,9) . rand(0,9);
    }else{
      $rnd = "1.000";
    }
    break;
  case 1:
    $rnd = 0.7;
    break;
  case 2:
    $rnd = 0.8;
    break;
}
echo "Random:  " . $rnd;


First it randomly chooses between returning either 0.8, 0.7 or any decimal value between 0 and 1.

EDIT:  I came back to add a comment and noticed a typo.  THIS code has been tested.
Link to comment
https://forums.phpfreaks.com/topic/26642-probabilities/#findComment-121903
Share on other sites

This one's got me thinkin'.  I like seeing what kind of unusual code I can come up with and what kinds of variations I can use. 

Anyway this one will allow you better control over the level of probabilty that you get the two values you're looking for.  And this version has been tested.

<?php
switch (rand(0,10)){  //<--The higher this second number is, the less likely you'll get one of the two values you're looking for. 
                            //    The lower the number, the better the chances.
  case 0:
    $rnd = 0.7;
    break;
  case 1:
    $rnd = 0.8;
    break;
  default:
    $rnd = round(rand(1,1000)/1000,3);
    break;
}
echo "Random:  " . $rnd;
?>
Link to comment
https://forums.phpfreaks.com/topic/26642-probabilities/#findComment-121945
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.