Jump to content

I hate math, Anyone?


jscix

Recommended Posts

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) :D

 

ty in advance

 

 




<!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>";
}
?>


Link to comment
https://forums.phpfreaks.com/topic/48554-i-hate-math-anyone/
Share on other sites

If anyone is interested/cares I figured it out.

(well, someone from www.physicsforums.com told me how to finish it.) ;o

 

All I was missing was dividing the deviation by the total number of coin tosses. If anyone wants to check it out ,

heres the 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);

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

if ($cur == "2") {
$dev--;
$two++;
}
$i++;
}
$st = ($dev / $total);

print "<center>ones: $one <br>twos: $two<br><br>";
print "<b>total: "  . $st . " point deviation.</b><br><br>";
print <<< DUH
Average deviation should be 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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/48554-i-hate-math-anyone/#findComment-238302
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.