Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

PHP Random number generator (Need help)

  1. Apr 24, 2007 #1
    Okay, What im trying to do is create a create a random number generator, that outputs 1 and 2 (coin toss)

    From what I understand, the more coin tosses (the more times I generate a random 1 or 2) the more uniform the pattern should be.

    EG: After generating a random 1-2 sequence, 1,000,000 times.. i should have 500,000 (1s) and 500,000 (2s).. am I wrong?

    Anyway, I'm not sure what Im doing wrong but the more numbers I generate, the more deviation Im getting.

    Here is how i'm doing this.. (I know I probably did this all wrong.. if so someone just tell me lols)

    thank you in advance

    (I know most wont know PHP but im providing the code, in case)


    basically what im doing is.

    I have a value that begins at 0. ( $dev )

    I generate a random 1 or 2

    if it's one, I increment the value ($dev++)
    if it's two, I decrement the value. ($dev--)

    the final deviation, displayed after it finishes generating the numbers.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Coin Toss</title>
    <html xmlns="http://www.w3.org/1999/xhtml">
    $submit = ($_POST['Toss']);
    $values = ($_POST['select']);
    if (!$submit) {
    <p align="center">Coin Toss Deviation
    <form method="post" action="Cointoss.php">
    <select name="select">
    <option value="none">How many coin tosses?</option>
    <option value="one">One Hundred</option>
    <option value="two">One Thousand</option>
    <option value="three">One-Hundred Thousand</option>
    </select><input type="submit" name="Toss" value="Toss" />

    else {

    switch ($values) {

    case "none":
    die ("Select number of tosses");
    case "one":
    $total = "100";
    case "two":
    $total = "1000";
    case "three":
    $total = "100000";
    $dev = 0;
    $i = 0;

    while ($i < $total) {

    $cur = mt_rand(1,2);
    //print $i . " = $cur <br>";

    if ($cur == "1") {

    if ($cur == "2") {
    unset ($submit);
    print "<center>ones: $one <br>twos: $two<br>";
    print "total: " . $dev . " point deviation.<br><br>";

    print <<< DUH
    Average deviation is 0 points<br>
    The more tosses, the lower the deviation should be. (Closer to 0)<br>

    print "<a href=\"Cointoss.php\">Try Again!</a>";
  2. jcsd
  3. Apr 24, 2007 #2
    The "uniformity" of your random numbers is geven by $dev/$count where $count is the number of numbers generated. This should tend to zero.
  4. Apr 24, 2007 #3


    User Avatar
    Science Advisor

    No that's exactly what you should expect. What you're looking at is a one dimensional random walk, the expected value of the absolute distance from the starting point is proportional to [tex]\sqrt{n}[/tex]

    See: http://en.wikipedia.org/wiki/Random_walk
    Last edited: Apr 25, 2007
  5. Apr 25, 2007 #4


    User Avatar
    Staff Emeritus
    Science Advisor

    After 1000000 tosses you certainly should not have 500000 heads and 500000 tails! That is the most likely outcome but there are so many different outcomes that the probability of getting that particular result is vanishingly small (it is 10000000C5000002-1000000).

    Nor should you expect the difference between the number of heads and tails to go to 0 as you increase the number of tosses. As uart said, that difference will actually increase, as [itex]\sqrt{n}[/itex], as n increases. What goes to 0 (by the "law of large numbers") is that difference as a ratio of the number of tosses: (difference between number of heads and tails)/n. Since the difference increases as [itex]\sqrt{n}[/itex], that ratio decreases as [itex]\sqrt{n}/n= 1/\sqrt{n}[/itex].
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: PHP Random number generator (Need help)
  1. True random numbers (Replies: 13)

  2. Psuedo-random numbers (Replies: 3)