2009-10-12 81 views
1

是否可以控制rand的輸出,例如,如果我只是想讓rand給出變量$ roll1的輸出,其值爲半數時間的值或數字1 rand運行或瀏覽器刷新時的可能性,如何實現?PHP控制rand的輸出

我的代碼糟透了,但我爲了學習而奮鬥,我只是偶爾得到一個,但這並不一致,我每次刷新頁面時都需要一個1。

因此,如果我刷新頁面6次,我應該從變量$ roll1中獲得1次1次,而$ roll1的其餘值應該是隨機的。

<!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> 

回答

6

你可以做這樣的事情

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

對於與我相同的答案+1,並且能夠輸入而不會意外提交表單。 – timdev 2009-10-12 23:25:36

4

不能直接使蘭特()做到這一點,但你可以做這樣的事情:

<?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. 
} 
+0

您忘記發佈您的代碼! – 2009-10-12 23:22:01

+1

在編寫增量答案時,您至少應該嘗試使中間修訂有幫助。 – Joey 2009-10-12 23:22:01

+0

是的,胖的。總是最終擊中答案框中的標籤,以某種方式在空格後面輸入。 – timdev 2009-10-12 23:24:18

0

略有不同解。它沒有那麼優雅,但可能更有利於更好地加載芯片?

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

你的代碼只給出三分之一的時間,它也可以給出7. – Grandpa 2009-10-12 23:56:50

+0

當我累了時爲我張貼答案的正確答案... – 2009-10-13 00:57:14

0

所以,如果你想保證,在過去的6個輥他們總是已經至少3個的,我想你會跟蹤卷的歷史。這是一種方法來做到這一點:

<?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>'; 
0

而不是調用rand()兩次,你可以簡單地做一些額外的數學。

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