PHP rand çıkışını kontrol

5 Cevap

Ben sadece rand koştum veya zaman altı olasılık arasından değeri ya da 1 buçuk sayısı seferinde bana değişken $ roll1 çıkışını vermek rand istiyorsanız örneğin rand çıkışını kontrol etmek mümkün olduğunda tarayıcı yenilenir, nasıl bir o başarmak nedir?

Benim kod berbat ama ben öğrenmek için mücadele ediyorum, ben sadece her şimdi ve sonra bir tane olsun, ama tutarlı değil, ben istiyorum ben 1 sayfayı yenileyin her zaman.

Ben sayfa 6 kez yenilerseniz Yani değişken $ roll1 üç kez dışarı 1 almalısınız, ve $ roll1 için değerleri kalanı rastgele olmalıdır.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">



<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<title>

loaded dice

</title>

</head>

<body>

<h1>loaded dice</h1>

<h3>loaded dice</h3>



<?php

// loaded dice, should roll the number 1 half the time out of a total of 6.
// So if I refreshed my browser six times I should at least see three 1's for roll1.

$roll1 = rand(1,6);

// Okay is it possible to divide rand by two or somehow set it up

// so that I get the the value 1 half the time?


// I am trying division here on the if clause in the hopes that I can just have

// 1 half the time, but it's not working,maybe some type of switch might work? :-(.

if ($roll1 == 3)

{

 $roll1/3;

}



if ($roll1 == 6)

{

 $roll1/6;

}



if ($roll1 == 1)

{

 $roll1/1;

}



// This parts works fine :-).

// Normal random roll, is okay.

$roll2 = rand(1,6);



print <<<HERE

<p>Rolls normal roll:</p>

You rolled a $roll2.

<p>Rolls the number 1 half the time:</p>

<p>You rolled a $roll1.</p>

HERE;

// Notice how we used $roll1 and 2, alongside the HERE doc to echo out a given value.

?>



<p>

Please refresh this page in the browser to roll another die.

</p>

</body>

</html>

5 Cevap

Böyle bir şey yapabileceğini

if (rand(0,1))
{
	$roll = rand(2,6);
}
else
{
	$roll = 1;
}

Doğrudan () bunu rand yapamaz, ama bu böyle bir şey yapabilirsiniz:

<?PHP
function roll(){
   if(rand(0,1))  //this should evaluate true half the time.
       return 1;
   return rand(2,6); //the other half of the time we want this.
}

Biraz farklı bir çözüm. O kadar şık değil, ama daha ince kalıp yükleme, belki de daha elverişli?

$i = rand(1, 9);
if($i<=3)
{
    $num = 1;
}
else $num = $i-2;

Her zaman en az 3 olanlar onların olurdu son 6 rulo garanti etmek istiyorsanız, ben size rulo geçmişini izlemek zorunda düşünüyorum. İşte bunu yapmak için bir yoldur:

<?php

if (array_key_exists('roll_history', $_GET)) {
    $rollHistory = unserialize($_GET['roll_history']);
} else {
    $rollHistory = array();
}

$oneCount = 0;
foreach($rollHistory as $roll) {
    if ($roll == 1) {
    	$oneCount++;
    }
}

if (6 - count($rollHistory) + $oneCount <= 3) {
    $roll = 1;
} else {
    if (rand(0,1)) {
        $roll = rand(2,6);
    } else {
        $roll = 1;
    }
}
$rollHistory[] = $roll;
if (count($rollHistory) > 5) {
    array_shift($rollHistory);
}

echo '<p>Weighted Dice Role: ' . $roll . '</p>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="get" >';
echo '<input type="hidden" name="roll_history" value="' . htmlspecialchars(serialize($rollHistory)) . '" />';
echo '<input type="submit" value="Roll Again" name="roll_again" />';
echo '</form>';

Aksine çağrı rand az () iki kez, sadece biraz ekstra matematik yapabilirsiniz.

$roll = $x = rand(1,12)-6 ? $x : 1;