PHP Random number generator (Need help)

Click For Summary

Discussion Overview

The discussion revolves around the implementation and behavior of a random number generator in PHP, specifically focused on simulating a coin toss that outputs either 1 or 2. Participants explore the expected uniformity of results over a large number of iterations and the observed deviations from this expectation.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the expected uniformity of results, suggesting that generating a large number of random numbers should yield an equal distribution of 1s and 2s.
  • Another participant clarifies that the uniformity can be measured by the ratio of deviation to the total count of generated numbers, which should approach zero.
  • A different participant notes that the observed deviation is expected due to the nature of a one-dimensional random walk, where the expected distance from the starting point increases with the square root of the number of tosses.
  • It is mentioned that while 500,000 heads and 500,000 tails is the most likely outcome after 1,000,000 tosses, the probability of achieving that exact result is very low, and the difference between heads and tails is expected to increase with the square root of the number of tosses.
  • Another participant emphasizes that the ratio of the difference between the number of heads and tails to the total number of tosses will decrease as the number of tosses increases.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the expectations of uniformity in the results of the random number generator. There are multiple competing views regarding the implications of the law of large numbers and the behavior of random walks.

Contextual Notes

Participants discuss the limitations of the assumptions made regarding uniformity and the mathematical principles governing random walks, but these remain unresolved within the discussion.

jscix
Messages
2
Reaction score
0
Okay, What I am 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 I am doing wrong but the more numbers I generate, the more deviation I am 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 won't know PHP but I am providing the code, in case)

OTHERWISE--

basically what I am 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.



Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Coin Toss</title>
</head>
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$submit = ($_POST['Toss']);
$values = ($_POST['select']);
if (!$submit) {
?>
<body><center>
<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" />
</form>
</center></body></p>

</html>
<?php
}
else {

switch ($values) {

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

while ($i < $total) {

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

if ($cur == "1") {
$dev++;
$one++;
}

if ($cur == "2") {
$dev--;
$two++;
}
$i++;
}
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>
DUH;

print "<a href=\"Cointoss.php\">Try Again!</a>";
}
?>
 
Last edited by a moderator:
Mathematics news on Phys.org
The "uniformity" of your random numbers is geven by $dev/$count where $count is the number of numbers generated. This should tend to zero.
 
Anyway, I'm not sure what I am doing wrong but the more numbers I generate, the more deviation I am getting.

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 \sqrt{n}

See: http://en.wikipedia.org/wiki/Random_walk
 
Last edited:
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 \sqrt{n}, 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 \sqrt{n}, that ratio decreases as \sqrt{n}/n= 1/\sqrt{n}.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
22
Views
5K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K