Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP controlling the output of rand
    text
    copied!<p>Is it possible to control the output of rand, for example if I just want rand to give me the output of the variable $roll1 with the value or number of 1 half the time out of the six possibilities when rand is ran or when the browser is refreshed, how does one accomplish that?</p> <p>My code sucks but I am fighting to learn, I only get one every now and then, but it's not consistent, I want a 1 every time I refresh the page.</p> <p>So If I refresh the page 6 times I should get a 1 out of the variable $roll1 three times, and the rest of the values for $roll1 should be random.</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt; &lt;title&gt;loaded dice&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;loaded dice&lt;/h1&gt; &lt;h3&gt;loaded dice&lt;/h3&gt; &lt;?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 &lt;&lt;&lt;HERE &lt;p&gt;Rolls normal roll:&lt;/p&gt; You rolled a $roll2. &lt;p&gt;Rolls the number 1 half the time:&lt;/p&gt; &lt;p&gt;You rolled a $roll1.&lt;/p&gt; HERE; // Notice how we used $roll1 and 2, alongside the HERE doc to echo out a given value. ?&gt; &lt;p&gt; Please refresh this page in the browser to roll another die. &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload