How to Create a PHP Random Number Generator for a Coin Toss?

In summary, the conversation discusses creating a random number generator for a coin toss, with the goal of obtaining a uniform pattern with equal amounts of 1s and 2s after a large number of tosses. However, the more numbers generated, the more deviation is observed. The provided code and explanation suggest that the issue lies in checking the absolute deviation rather than the relative deviation.
  • #1
jscix
2
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) Cheesy

thank you in advance


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:
Technology news on Phys.org
  • #2
jscix said:
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?
It will never be exact.

The problem in the OP is that one has to check the relative, not absolute, deviation.
 

FAQ: How to Create a PHP Random Number Generator for a Coin Toss?

How does the PHP random number generator work?

The PHP random number generator works by using a mathematical algorithm to generate a sequence of random numbers. This algorithm is based on a seed value, which is used to initialize the generator. The generator then uses this seed value to generate a seemingly random sequence of numbers.

Is the PHP random number generator truly random?

No, the PHP random number generator is not truly random. It uses a mathematical algorithm to generate a sequence of numbers, which means that the numbers are not truly random. However, for most practical purposes, the generated numbers are random enough.

Can I influence the range of numbers generated by the PHP random number generator?

Yes, you can influence the range of numbers generated by the PHP random number generator by using the mt_rand() function. This function allows you to specify a minimum and maximum value for the generated numbers. You can also use other functions like rand() and mt_getrandmax() to further manipulate the range of numbers.

How can I ensure that the PHP random number generator produces unique numbers?

To ensure that the PHP random number generator produces unique numbers, you can use a combination of the mt_rand() function and the array_unique() function. The mt_rand() function will generate a random number, and the array_unique() function will remove any duplicates from an array of numbers.

Can the PHP random number generator be used for cryptography?

No, the PHP random number generator is not suitable for cryptography. It is not designed to produce truly random numbers, which are necessary for secure encryption. For cryptographic purposes, it is recommended to use a specialized random number generator library.

Similar threads

Back
Top