Можно ли управлять выходом rand, например, если я просто хочу, чтобы rand дал мне выход переменной $ roll1 со значением или числом в 1 половину времени из шести когда запускается rand или когда браузер обновляется, как это выполнить?PHP, управляющий выходом rand
Мой код сосет, но я борюсь за то, чтобы учиться, я получаю его каждый раз, но это не согласуется, я хочу 1 каждый раз, когда обновляю страницу.
Так что если я обновляю страницу 6 раз, я должен получить 1 из переменной $ roll1 три раза, а остальные значения для $ 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