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>
+1 나와 같은 대답을하고 실수로 양식을 제출하지 않고 입력 할 수 있습니다. – timdev