2009-10-12 4 views
1

rand의 출력을 제어 할 수 있습니까? 예를 들어, rand가 $ roll1 변수의 출력에 6의 시간 중 절반의 값 또는 1의 값을 주길 원한다면 rand가 실행될 때 또는 브라우저가 새로 고쳐질 때의 가능성은 어떻게 달성 할 수 있습니까?PHP가 rand의 출력을 제어하는 ​​경우

코드가 엉망이지만 배우기 위해 싸우고 있습니다. 매 순간마다 하나씩 만 얻지 만 일관성이 없습니다. 페이지를 새로 고칠 때마다 1을 원합니다.

그래서 페이지를 6 번 새로 고치면 $ roll1 변수에서 3 번 나가야하고 나머지 $ 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

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

코드를 게시하는 것을 잊었습니다! –

+1

증 분식 대답을 쓸 때 최소한 중간 버전을 유용하게 만들어야합니다. – Joey

+0

그래, 뚱뚱한 손가락. 결국 대답 상자에 탭을 두드려라. 어쨌든 빈 칸을 입력하거나 입력하십시오. – timdev

0

다른 약간처럼 뭔가를 할 수를 해결책. 그것은 우아하지는 않지만, 아마도 더 정밀하게 금형을 적재하는 데 더 도움이 될 것입니다.

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

당신의 코드는 시간의 3 분의 1 밖에주지 않으며, 또한 7을 줄 수 있습니다. – Grandpa

+0

피곤하면 답변을 게시 할 수있게 해줍니다 ... –

0

그래서 당신은 항상 최소한 3 사람 그들의했을 것이다 지난 6 롤 것을 보장하려는 경우, 난 당신이 롤의 역사를 추적해야합니다 생각합니다. 다음과 같이 할 수 있습니다.

<?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; 
관련 문제