如何在PHP中操作rand()的范围


How to manipulate the range of rand() in PHP?

好的,所以我正在制作一个小的赌博网站进行练习,我想有"高滚"或"低滚"的选项

例如:如果投注者选择20%的获胜机会,并且他们选择高滚,他们将以80-100获胜。如果他们选择低滚,他们将以1-20获胜。现在,我在下面的代码总是滚动"低",就像在1-20中一样,如果获胜几率为20%。

我觉得这并不难做到,但它让我很困惑。:s哦,如果你要举一个例子,请使用下面的代码,除了使用isset($_POST['start2']),并更改其他需要更改的代码以使其工作。

谢谢。

$rand = rand(100, 10000)/100;
if(isset($_POST['start1'])) {
if(isset($_POST['bet'], $_POST['pay'], $_POST['profit'], $_POST['chance'])) {
   if($rand < $_POST['chance']) {
 echo '<h3>You rolled a <strong>' .$rand. ' </strong> out of 100 on the percentile dice!  You won!</h3>';
}
else if($rand > $_POST['chance']) { echo '<h3>You rolled a <strong>' .$rand. '</strong> out of 100 on the percentile dice!  You lost...</h3>'; }
}
}

我看不出从用户的POST中可以从哪里获得高/低选择。得到那个,然后,如果他们选择高,比较100的机会。

例如:

  $rand = rand(0,100);
  $chance = $_POST['chance'];
  $roll = "L";     //should come from a POST variable set to H or L
  if($roll == 'H')
      $win = ($rand + $chance) - 100;
  else
      $win = $chance - $rand;
  if($win > 0)
      echo "You rolled a " . $rand . " You win!";
  else
      echo "You rolled a " . $rand . " You lose.";