# Ok so this is random

• Proficient
• Posts: 481

3+ Months Ago

Sometimes I'm torn between decisions when this usually happens I leave it up to a coin flip. When I flip a coin I do a best 2 of 3 of 2 of 3.

example of 2 of 3 of 2 of 3

3) tails
5) tails

TAILS!

6) tails
8) tails

TAILS!

So I go with tails.

So whats the point? The point is today I was bored and couldn't make a decision at work so I wrote a program that will "Filp" a coin 10,000 times it will then spit out the number of times it lands on yes and no, i used that instead of heads and tails. it also tells the percentage it landed on yes and no.

Also if shows what the first flip was and the first 2 out of three results as well as the the 2 of 3 of 2 of 3, but I didn't stop there I also added in the best 3 of 5 and 3 of 5 of 3 of 5.

This was done in PHP
PHP Code: [ Select ]
<?php

// Number of flips
\$NoF = 10000;

// Make an array to hold the counts
\$counts = array("yes" => 0, "no" => 0);

// First toss
\$tosses = array();

// 2 of 3
\$best_2_of_3 = array("yes" => 0, "no" => 0);

// 3 by 3
\$best_3_by_3 = array("yes" => 0, "no" => 0);

// 3 of 5
\$best_3_of_5 = array("yes" => 0, "no" => 0);

// 5 by 5
\$best_5_by_5 = array("yes" => 0, "no" => 0);

// Start Fliping out
for(\$i=0; \$i<\$NoF; \$i++) {

// Do a rand and check the answer
if(rand(0, 1)) {

\$counts['yes']++;

// Add to the 2 of 3 and 3 of 5
\$best_2_of_3['yes']++;
\$best_3_of_5['yes']++;

} else {

\$counts['no']++;

// Add to the 2 of 3 and 3 of 5
\$best_2_of_3['no']++;
\$best_3_of_5['no']++;

}

// Check for first toss
if(empty(\$tosses['first'])) {

// Set the \$first_toss
\$tosses['first'] = (!empty(\$counts['yes']) ? 'Yes': 'No');

}

// Check for 3by3
if(empty(\$tosses['3by3'])) {

// Check the count of the 2 of 3
if(\$best_2_of_3['yes'] == 2 || \$best_2_of_3['no'] == 2) {

// Figure out if it is yes or no
\$result = (\$best_2_of_3['yes'] == 2) ? 'yes' : 'no';

// Check for first grouping
if(empty(\$tosses['2of3'])) {

// Set the 2 of 3
\$tosses['2of3'] = ucwords(\$result);

}

\$best_3_by_3[\$result]++;

// Reset the \$best_2_of_3
\$best_2_of_3['yes'] = 0;
\$best_2_of_3['no'] = 0;

// Check the 3by3
if(\$best_3_by_3['yes'] == 2 || \$best_3_by_3['no'] == 2) {

// Figure out if it is yes or no
\$result = (\$best_3_by_3['yes'] == 2) ? 'yes' : 'no';

// Set the 3 by 3
\$tosses['3by3'] = ucwords(\$result);

}

}

}

// Check for 5by5
if(empty(\$tosses['5by5'])) {

// Check the count of the 3 of 5
if(\$best_3_of_5['yes'] == 3 || \$best_3_of_5['no'] == 3) {

// Figure out if it is yes or no
\$result = (\$best_3_of_5['yes'] == 3) ? 'yes' : 'no';

// Check for first grouping
if(empty(\$tosses['3of5'])) {

// Set the 2 of 3
\$tosses['3of5'] = ucwords(\$result);

}

\$best_5_by_5[\$result]++;

// Reset the \$best_3_of_5
\$best_3_of_5['yes'] = 0;
\$best_3_of_5['no'] = 0;

// Check the 3by3
if(\$best_5_by_5['yes'] == 3 || \$best_5_by_5['no'] == 3) {

// Figure out if it is yes or no
\$result = (\$best_5_by_5['yes'] == 2) ? 'yes' : 'no';

// Set the 5 by 5
\$tosses['5by5'] = ucwords(\$result);

}

}

}

}

// Total counts
\$total_counts = \$counts['yes'] + \$counts['no'];

// Calculate percentages
\$percents = array("yes" => ((\$counts['yes']/\$total_counts) * 100), "no" => ((\$counts['no']/\$total_counts) * 100))

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Decisions Decisions</title>
<style type="text/css">

span.result {
font-weight:bold;
color:#F00;
}

</style>

<body>
<strong>Results</strong>
<br />
The coin was flipped <span class="result"><?php echo \$total_counts; ?></span> times. It landed on <strong>Yes</strong> <span class="result"><?php echo \$counts['yes']; ?></span> times and <strong>No</strong> <span class="result"><?php echo \$counts['no']; ?></span> times.<br />
<br />
Percents<br />
<strong>Yes</strong> = <span class="result"><?php echo \$percents['yes']; ?><br /></span>
<strong>No</strong> = <span class="result"><?php echo \$percents['no']; ?><br /></span>
<br />
<strong>First Toss</strong> <span class="result"><?php echo \$tosses['first']; ?></span>
<br />
<strong>Best 2 of 3</strong> <span class="result"><?php echo \$tosses['2of3']; ?></span>
<br />
<strong>Best 3 by 3</strong> <span class="result"><?php echo \$tosses['3by3']; ?></span>
<br />
<strong>Best 3 of 5</strong> <span class="result"><?php echo \$tosses['3of5']; ?></span>
<br />
<strong>Best 5 by 5</strong> <span class="result"><?php echo \$tosses['5by5']; ?></span>

</body>
</html>

1. <?php
2.
3. // Number of flips
4. \$NoF = 10000;
5.
6. // Make an array to hold the counts
7. \$counts = array("yes" => 0, "no" => 0);
8.
9. // First toss
10. \$tosses = array();
11.
12. // 2 of 3
13. \$best_2_of_3 = array("yes" => 0, "no" => 0);
14.
15. // 3 by 3
16. \$best_3_by_3 = array("yes" => 0, "no" => 0);
17.
18. // 3 of 5
19. \$best_3_of_5 = array("yes" => 0, "no" => 0);
20.
21. // 5 by 5
22. \$best_5_by_5 = array("yes" => 0, "no" => 0);
23.
24. // Start Fliping out
25. for(\$i=0; \$i<\$NoF; \$i++) {
26.
27.    // Do a rand and check the answer
28.    if(rand(0, 1)) {
29.
31.       \$counts['yes']++;
32.
33.       // Add to the 2 of 3 and 3 of 5
34.       \$best_2_of_3['yes']++;
35.       \$best_3_of_5['yes']++;
36.
37.
38.    } else {
39.
41.       \$counts['no']++;
42.
43.       // Add to the 2 of 3 and 3 of 5
44.       \$best_2_of_3['no']++;
45.       \$best_3_of_5['no']++;
46.
47.    }
48.
49.    // Check for first toss
50.    if(empty(\$tosses['first'])) {
51.
52.       // Set the \$first_toss
53.       \$tosses['first'] = (!empty(\$counts['yes']) ? 'Yes': 'No');
54.
55.    }
56.
57.    // Check for 3by3
58.    if(empty(\$tosses['3by3'])) {
59.
60.       // Check the count of the 2 of 3
61.       if(\$best_2_of_3['yes'] == 2 || \$best_2_of_3['no'] == 2) {
62.
63.          // Figure out if it is yes or no
64.          \$result = (\$best_2_of_3['yes'] == 2) ? 'yes' : 'no';
65.
66.          // Check for first grouping
67.          if(empty(\$tosses['2of3'])) {
68.
69.             // Set the 2 of 3
70.             \$tosses['2of3'] = ucwords(\$result);
71.
72.          }
73.
74.          // Add to the \$best_3_by_3
75.          \$best_3_by_3[\$result]++;
76.
77.          // Reset the \$best_2_of_3
78.          \$best_2_of_3['yes'] = 0;
79.          \$best_2_of_3['no'] = 0;
80.
81.          // Check the 3by3
82.          if(\$best_3_by_3['yes'] == 2 || \$best_3_by_3['no'] == 2) {
83.
84.             // Figure out if it is yes or no
85.             \$result = (\$best_3_by_3['yes'] == 2) ? 'yes' : 'no';
86.
87.             // Set the 3 by 3
88.             \$tosses['3by3'] = ucwords(\$result);
89.
90.          }
91.
92.       }
93.
94.    }
95.
96.    // Check for 5by5
97.    if(empty(\$tosses['5by5'])) {
98.
99.       // Check the count of the 3 of 5
100.       if(\$best_3_of_5['yes'] == 3 || \$best_3_of_5['no'] == 3) {
101.
102.          // Figure out if it is yes or no
103.          \$result = (\$best_3_of_5['yes'] == 3) ? 'yes' : 'no';
104.
105.          // Check for first grouping
106.          if(empty(\$tosses['3of5'])) {
107.
108.             // Set the 2 of 3
109.             \$tosses['3of5'] = ucwords(\$result);
110.
111.          }
112.
113.          // Add to the \$best_5_by_5
114.          \$best_5_by_5[\$result]++;
115.
116.          // Reset the \$best_3_of_5
117.          \$best_3_of_5['yes'] = 0;
118.          \$best_3_of_5['no'] = 0;
119.
120.          // Check the 3by3
121.          if(\$best_5_by_5['yes'] == 3 || \$best_5_by_5['no'] == 3) {
122.
123.             // Figure out if it is yes or no
124.             \$result = (\$best_5_by_5['yes'] == 2) ? 'yes' : 'no';
125.
126.             // Set the 5 by 5
127.             \$tosses['5by5'] = ucwords(\$result);
128.
129.          }
130.
131.       }
132.
133.    }
134.
135. }
136.
137. // Total counts
138. \$total_counts = \$counts['yes'] + \$counts['no'];
139.
140. // Calculate percentages
141. \$percents = array("yes" => ((\$counts['yes']/\$total_counts) * 100), "no" => ((\$counts['no']/\$total_counts) * 100))
142.
143. ?>
144.
145.
146. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
147. <html xmlns="http://www.w3.org/1999/xhtml">
149. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
150. <title>Decisions Decisions</title>
151. <style type="text/css">
152.
153. span.result {
154.    font-weight:bold;
155.    color:#F00;
156. }
157.
158. </style>
160.
161. <body>
162. <strong>Results</strong>
163. <br />
164. The coin was flipped <span class="result"><?php echo \$total_counts; ?></span> times. It landed on <strong>Yes</strong> <span class="result"><?php echo \$counts['yes']; ?></span> times and <strong>No</strong> <span class="result"><?php echo \$counts['no']; ?></span> times.<br />
165. <br />
166. Percents<br />
167. <strong>Yes</strong> = <span class="result"><?php echo \$percents['yes']; ?><br /></span>
168. <strong>No</strong> = <span class="result"><?php echo \$percents['no']; ?><br /></span>
169. <br />
170. <strong>First Toss</strong> <span class="result"><?php echo \$tosses['first']; ?></span>
171. <br />
172. <strong>Best 2 of 3</strong> <span class="result"><?php echo \$tosses['2of3']; ?></span>
173. <br />
174. <strong>Best 3 by 3</strong> <span class="result"><?php echo \$tosses['3by3']; ?></span>
175. <br />
176. <strong>Best 3 of 5</strong> <span class="result"><?php echo \$tosses['3of5']; ?></span>
177. <br />
178. <strong>Best 5 by 5</strong> <span class="result"><?php echo \$tosses['5by5']; ?></span>
179.
180. </body>
181. </html>
182.
• Brewmaster
• Posts: 6271
• Loc: Seattle, WA

3+ Months Ago

For the record, assuming your coin isn't special (or in this case, that your random number generator is reliably pseudo-random), there's no mathematically practical difference between flipping a single coin and using the result, or counting the results of successive coin flips.

Edit: now that I think about it, I'm pretty sure that most of PHP's design decisions were made with a coin flip.
• Posts: 131

3+ Months Ago

Maybe add a script that would let us know if one side comes up more than 10+ times in a row. Or show the results of the most times in a row that each side produced (sorry about the wording there). Then you're making your decision based on something other than just a 50/50 outcome (well technically it's still 50/50 on which side would produce such a result). And I just think it would be cool to see the how many times heads or tails can come up in a row, when it's being flipped 10,000 times. It would also let us know if it is actually "pseudo-random," because in real life this would eventually happen. I think it's about 1 in 1000 chance of getting one side 10 times in a row, so this should be fairly common.
• Proficient
• Posts: 481

3+ Months Ago

I'm not sure if it is reliably pseudo-random it was taking the a random 0 or 1 i have changed the code a bit since then now i'm using mt_rand instead and also its not trying to find a random 1 or 0 but a random number between 1 and the \$NoF variable which is 10000 at that point i check for an even or odd number to give me my coin side.

Also added in the the 10+ times in a row this morning.

Just a note though this was something i wrote simply to burn time I have several of these "time burner" scripts in different coding languages. Many i forget about though lol and then i find them later. Like the analog flash clock and the php encoder/decoder and php "BoMB" I made.

PHP Code: [ Select ]
<?php

// Number of flips
\$NoF = 10000;

// Make an array to hold the counts
\$counts = array("yes" => 0, "no" => 0);

// First toss
\$tosses = array();

// 2 of 3
\$best_2_of_3 = array("yes" => 0, "no" => 0);

// 3 by 3
\$best_3_by_3 = array("yes" => 0, "no" => 0);

// 3 of 5
\$best_3_of_5 = array("yes" => 0, "no" => 0);

// 5 by 5
\$best_5_by_5 = array("yes" => 0, "no" => 0);

// Final count
\$final_count = array("yes" => 0, "no" => 0);

// In a row
\$in_counter = array("yes" => 0, "no" => 0);
\$last_result;
\$in_a_row = array();

// Start Fliping out
for(\$i=0; \$i<\$NoF; \$i++) {

// Do a rand
\$rand = mt_rand(1, \$NoF);

// Check the answer if it is odd or even
if(\$rand % 2) {

\$counts['yes']++;

// Add to the 2 of 3 and 3 of 5
\$best_2_of_3['yes']++;
\$best_3_of_5['yes']++;

// Set the \$in_counter
\$in_counter['yes']++;

// Check last_result
if(\$last_result == 'no') {

// Check the \$in_counter
if(\$in_counter['no'] >= 10) {

// Set the \$in_a_row
\$in_a_row[] = array("No", \$in_counter['no']);

}

// Set the \$in_counter
\$in_counter['no'] = 0;
}

// Set the last result
\$last_result = 'yes';

} else {

\$counts['no']++;

// Add to the 2 of 3 and 3 of 5
\$best_2_of_3['no']++;
\$best_3_of_5['no']++;

// Set the \$in_counter
\$in_counter['no']++;

// Check last_result
if(\$last_result == 'yes') {

// Check the \$in_counter
if(\$in_counter['yes'] >= 10) {

// Set the \$in_a_row
\$in_a_row[] = array("Yes", \$in_counter['yes']);

}

// Set the \$in_counter
\$in_counter['yes'] = 0;
}

// Set the last result
\$last_result = 'no';

}

// Check for first toss
if(empty(\$tosses['first'])) {

// Set the \$first_toss
\$tosses['first'] = (!empty(\$counts['yes']) ? 'Yes': 'No');

}

// Check for 3by3
if(empty(\$tosses['3by3'])) {

// Check the count of the 2 of 3
if(\$best_2_of_3['yes'] == 2 || \$best_2_of_3['no'] == 2) {

// Figure out if it is yes or no
\$result = (\$best_2_of_3['yes'] == 2) ? 'yes' : 'no';

// Check for first grouping
if(empty(\$tosses['2of3'])) {

// Set the 2 of 3
\$tosses['2of3'] = ucwords(\$result);

}

\$best_3_by_3[\$result]++;

// Reset the \$best_2_of_3
\$best_2_of_3['yes'] = 0;
\$best_2_of_3['no'] = 0;

// Check the 3by3
if(\$best_3_by_3['yes'] == 2 || \$best_3_by_3['no'] == 2) {

// Figure out if it is yes or no
\$result = (\$best_3_by_3['yes'] == 2) ? 'yes' : 'no';

// Set the 3 by 3
\$tosses['3by3'] = ucwords(\$result);

}

}

}

// Check for 5by5
if(empty(\$tosses['5by5'])) {

// Check the count of the 3 of 5
if(\$best_3_of_5['yes'] == 3 || \$best_3_of_5['no'] == 3) {

// Figure out if it is yes or no
\$result = (\$best_3_of_5['yes'] == 3) ? 'yes' : 'no';

// Check for first grouping
if(empty(\$tosses['3of5'])) {

// Set the 2 of 3
\$tosses['3of5'] = ucwords(\$result);

}

\$best_5_by_5[\$result]++;

// Reset the \$best_3_of_5
\$best_3_of_5['yes'] = 0;
\$best_3_of_5['no'] = 0;

// Check the 3by3
if(\$best_5_by_5['yes'] == 3 || \$best_5_by_5['no'] == 3) {

// Figure out if it is yes or no
\$result = (\$best_5_by_5['yes'] == 2) ? 'yes' : 'no';

// Set the 5 by 5
\$tosses['5by5'] = ucwords(\$result);

}

}

}

}

// Total counts
\$total_counts = \$counts['yes'] + \$counts['no'];

// Calculate percentages
\$percents = array("yes" => ((\$counts['yes']/\$total_counts) * 100), "no" => ((\$counts['no']/\$total_counts) * 100));

// Over all of the NoF
\$over_all = (\$counts['yes'] > \$counts['no']) ? 'Yes' : 'No';

// Do final count
(\$over_all == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
(\$tosses['first'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
(\$tosses['2of3'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
(\$tosses['3by3'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
(\$tosses['3of5'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
(\$tosses['5by5'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;

// In a row results
\$in_a_row_resluts = array();

// Count the yes and no's in a row
foreach(\$in_a_row as \$key => \$value) {

(\$value[0] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;

// Spit out the ina row numbers
\$in_a_row_resluts[] = '<strong>'. \$value[0] . '</strong> Came up <span class="result">' . \$value[1] . '</span> times<br />';

}

\$result = (\$final_count['yes'] > \$final_count['no']) ? 'Yes' : 'No';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Decisions Decisions</title>
<style type="text/css">

span.result {
font-weight:bold;
color:#F00;
}

</style>

<body>
<strong>Results</strong>
<br />
The coin was flipped <span class="result"><?php echo \$total_counts; ?></span> times. It landed on <strong>Yes</strong> <span class="result"><?php echo \$counts['yes']; ?></span> times and <strong>No</strong> <span class="result"><?php echo \$counts['no']; ?></span> times.<br />
<br />
Percents<br />
<strong>Yes</strong> = <span class="result"><?php echo \$percents['yes']; ?><br /></span>
<strong>No</strong> = <span class="result"><?php echo \$percents['no']; ?><br /></span>
<br />
<strong>First Toss</strong> <span class="result"><?php echo \$tosses['first']; ?></span>
<br />
<strong>Best 2 of 3</strong> <span class="result"><?php echo \$tosses['2of3']; ?></span>
<br />
<strong>Best 3 by 3</strong> <span class="result"><?php echo \$tosses['3by3']; ?></span>
<br />
<strong>Best 3 of 5</strong> <span class="result"><?php echo \$tosses['3of5']; ?></span>
<br />
<strong>Best 5 by 5</strong> <span class="result"><?php echo \$tosses['5by5']; ?></span>
<br />
<br />
<strong>Coin filps in a row over 10</strong>
<br />
<?php echo implode("\r\n", \$in_a_row_resluts); ?>
<br />
<br />
<?php echo (!empty(\$_REQUEST['q']) ? \$_REQUEST['q'] . '<br />' : ''); ?>
<span class="result"><?php echo \$result; ?></span>

</body>
</html>

1. <?php
2.
3. // Number of flips
4. \$NoF = 10000;
5.
6. // Make an array to hold the counts
7. \$counts = array("yes" => 0, "no" => 0);
8.
9. // First toss
10. \$tosses = array();
11.
12. // 2 of 3
13. \$best_2_of_3 = array("yes" => 0, "no" => 0);
14.
15. // 3 by 3
16. \$best_3_by_3 = array("yes" => 0, "no" => 0);
17.
18. // 3 of 5
19. \$best_3_of_5 = array("yes" => 0, "no" => 0);
20.
21. // 5 by 5
22. \$best_5_by_5 = array("yes" => 0, "no" => 0);
23.
24. // Final count
25. \$final_count = array("yes" => 0, "no" => 0);
26.
27. // In a row
28. \$in_counter = array("yes" => 0, "no" => 0);
29. \$last_result;
30. \$in_a_row = array();
31.
32. // Start Fliping out
33. for(\$i=0; \$i<\$NoF; \$i++) {
34.
35.    // Do a rand
36.    \$rand = mt_rand(1, \$NoF);
37.
38.    // Check the answer if it is odd or even
39.    if(\$rand % 2) {
40.
42.       \$counts['yes']++;
43.
44.       // Add to the 2 of 3 and 3 of 5
45.       \$best_2_of_3['yes']++;
46.       \$best_3_of_5['yes']++;
47.
48.       // Set the \$in_counter
49.       \$in_counter['yes']++;
50.
51.       // Check last_result
52.       if(\$last_result == 'no') {
53.
54.          // Check the \$in_counter
55.          if(\$in_counter['no'] >= 10) {
56.
57.             // Set the \$in_a_row
58.             \$in_a_row[] = array("No", \$in_counter['no']);
59.
60.          }
61.
62.          // Set the \$in_counter
63.          \$in_counter['no'] = 0;
64.       }
65.
66.       // Set the last result
67.       \$last_result = 'yes';
68.
69.    } else {
70.
72.       \$counts['no']++;
73.
74.       // Add to the 2 of 3 and 3 of 5
75.       \$best_2_of_3['no']++;
76.       \$best_3_of_5['no']++;
77.
78.       // Set the \$in_counter
79.       \$in_counter['no']++;
80.
81.       // Check last_result
82.       if(\$last_result == 'yes') {
83.
84.          // Check the \$in_counter
85.          if(\$in_counter['yes'] >= 10) {
86.
87.             // Set the \$in_a_row
88.             \$in_a_row[] = array("Yes", \$in_counter['yes']);
89.
90.          }
91.
92.          // Set the \$in_counter
93.          \$in_counter['yes'] = 0;
94.       }
95.
96.       // Set the last result
97.       \$last_result = 'no';
98.
99.
100.    }
101.
102.    // Check for first toss
103.    if(empty(\$tosses['first'])) {
104.
105.       // Set the \$first_toss
106.       \$tosses['first'] = (!empty(\$counts['yes']) ? 'Yes': 'No');
107.
108.    }
109.
110.    // Check for 3by3
111.    if(empty(\$tosses['3by3'])) {
112.
113.       // Check the count of the 2 of 3
114.       if(\$best_2_of_3['yes'] == 2 || \$best_2_of_3['no'] == 2) {
115.
116.          // Figure out if it is yes or no
117.          \$result = (\$best_2_of_3['yes'] == 2) ? 'yes' : 'no';
118.
119.          // Check for first grouping
120.          if(empty(\$tosses['2of3'])) {
121.
122.             // Set the 2 of 3
123.             \$tosses['2of3'] = ucwords(\$result);
124.
125.          }
126.
127.          // Add to the \$best_3_by_3
128.          \$best_3_by_3[\$result]++;
129.
130.          // Reset the \$best_2_of_3
131.          \$best_2_of_3['yes'] = 0;
132.          \$best_2_of_3['no'] = 0;
133.
134.          // Check the 3by3
135.          if(\$best_3_by_3['yes'] == 2 || \$best_3_by_3['no'] == 2) {
136.
137.             // Figure out if it is yes or no
138.             \$result = (\$best_3_by_3['yes'] == 2) ? 'yes' : 'no';
139.
140.             // Set the 3 by 3
141.             \$tosses['3by3'] = ucwords(\$result);
142.
143.          }
144.
145.       }
146.
147.    }
148.
149.    // Check for 5by5
150.    if(empty(\$tosses['5by5'])) {
151.
152.       // Check the count of the 3 of 5
153.       if(\$best_3_of_5['yes'] == 3 || \$best_3_of_5['no'] == 3) {
154.
155.          // Figure out if it is yes or no
156.          \$result = (\$best_3_of_5['yes'] == 3) ? 'yes' : 'no';
157.
158.          // Check for first grouping
159.          if(empty(\$tosses['3of5'])) {
160.
161.             // Set the 2 of 3
162.             \$tosses['3of5'] = ucwords(\$result);
163.
164.          }
165.
166.          // Add to the \$best_5_by_5
167.          \$best_5_by_5[\$result]++;
168.
169.          // Reset the \$best_3_of_5
170.          \$best_3_of_5['yes'] = 0;
171.          \$best_3_of_5['no'] = 0;
172.
173.          // Check the 3by3
174.          if(\$best_5_by_5['yes'] == 3 || \$best_5_by_5['no'] == 3) {
175.
176.             // Figure out if it is yes or no
177.             \$result = (\$best_5_by_5['yes'] == 2) ? 'yes' : 'no';
178.
179.             // Set the 5 by 5
180.             \$tosses['5by5'] = ucwords(\$result);
181.
182.          }
183.
184.       }
185.
186.    }
187.
188. }
189.
190. // Total counts
191. \$total_counts = \$counts['yes'] + \$counts['no'];
192.
193. // Calculate percentages
194. \$percents = array("yes" => ((\$counts['yes']/\$total_counts) * 100), "no" => ((\$counts['no']/\$total_counts) * 100));
195.
196. // Over all of the NoF
197. \$over_all = (\$counts['yes'] > \$counts['no']) ? 'Yes' : 'No';
198.
199. // Do final count
200. (\$over_all == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
201. (\$tosses['first'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
202. (\$tosses['2of3'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
203. (\$tosses['3by3'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
204. (\$tosses['3of5'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
205. (\$tosses['5by5'] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
206.
207. // In a row results
208. \$in_a_row_resluts = array();
209.
210. // Count the yes and no's in a row
211. foreach(\$in_a_row as \$key => \$value) {
212.
214.    (\$value[0] == 'Yes') ? \$final_count['yes']++ : \$final_count['no']++;
215.
216.    // Spit out the ina row numbers
217.    \$in_a_row_resluts[] = '<strong>'. \$value[0] . '</strong> Came up <span class="result">' . \$value[1] . '</span> times<br />';
218.
219. }
220.
221. // And The answer is
222. \$result = (\$final_count['yes'] > \$final_count['no']) ? 'Yes' : 'No';
223.
224. ?>
225.
226.
227. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
228. <html xmlns="http://www.w3.org/1999/xhtml">
230. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
231. <title>Decisions Decisions</title>
232. <style type="text/css">
233.
234. span.result {
235.    font-weight:bold;
236.    color:#F00;
237. }
238.
239. </style>
241.
242. <body>
243. <strong>Results</strong>
244. <br />
245. The coin was flipped <span class="result"><?php echo \$total_counts; ?></span> times. It landed on <strong>Yes</strong> <span class="result"><?php echo \$counts['yes']; ?></span> times and <strong>No</strong> <span class="result"><?php echo \$counts['no']; ?></span> times.<br />
246. <br />
247. Percents<br />
248. <strong>Yes</strong> = <span class="result"><?php echo \$percents['yes']; ?><br /></span>
249. <strong>No</strong> = <span class="result"><?php echo \$percents['no']; ?><br /></span>
250. <br />
251. <strong>First Toss</strong> <span class="result"><?php echo \$tosses['first']; ?></span>
252. <br />
253. <strong>Best 2 of 3</strong> <span class="result"><?php echo \$tosses['2of3']; ?></span>
254. <br />
255. <strong>Best 3 by 3</strong> <span class="result"><?php echo \$tosses['3by3']; ?></span>
256. <br />
257. <strong>Best 3 of 5</strong> <span class="result"><?php echo \$tosses['3of5']; ?></span>
258. <br />
259. <strong>Best 5 by 5</strong> <span class="result"><?php echo \$tosses['5by5']; ?></span>
260. <br />
261. <br />
262. <strong>Coin filps in a row over 10</strong>
263. <br />
264. <?php echo implode("\r\n", \$in_a_row_resluts); ?>
265. <br />
266. <br />
267. <?php echo (!empty(\$_REQUEST['q']) ? \$_REQUEST['q'] . '<br />' : ''); ?>
268. <span class="result"><?php echo \$result; ?></span>
269.
270. </body>
271. </html>
272.
• Proficient
• Posts: 481

3+ Months Ago

Random tests

PHP rand function
PHP Code: [ Select ]

\$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
\$white = imagecolorallocate(\$im, 255, 255, 255);
for (\$y = 0; \$y < 512; \$y++) {
for (\$x = 0; \$x < 512; \$x++) {
if (rand(0,1)) {
imagesetpixel(\$im, \$x, \$y, \$white);
}
}
}

imagepng(\$im);
imagedestroy(\$im);

2.
3. \$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
4. \$white = imagecolorallocate(\$im, 255, 255, 255);
5. for (\$y = 0; \$y < 512; \$y++) {
6.     for (\$x = 0; \$x < 512; \$x++) {
7.         if (rand(0,1)) {
8.             imagesetpixel(\$im, \$x, \$y, \$white);
9.         }
10.     }
11. }
12.
13. imagepng(\$im);
14. imagedestroy(\$im);
15.

Not so random http://www.indefinitedesigns.com/randtest1.png

PHP rand finding even or odd numbers
PHP Code: [ Select ]

\$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
\$white = imagecolorallocate(\$im, 255, 255, 255);
for (\$y = 0; \$y < 512; \$y++) {
for (\$x = 0; \$x < 512; \$x++) {
\$rand = rand(1, 512);
if (\$rand % 2) {
imagesetpixel(\$im, \$x, \$y, \$white);
}
}
}

imagepng(\$im);
imagedestroy(\$im);

2.
3. \$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
4. \$white = imagecolorallocate(\$im, 255, 255, 255);
5. for (\$y = 0; \$y < 512; \$y++) {
6.     for (\$x = 0; \$x < 512; \$x++) {
7.         \$rand = rand(1, 512);
8.         if (\$rand % 2) {
9.             imagesetpixel(\$im, \$x, \$y, \$white);
10.         }
11.     }
12. }
13.
14. imagepng(\$im);
15. imagedestroy(\$im);
16.

Umm What? I said random not a barcode http://www.indefinitedesigns.com/randtest4.png

PHP mt_rand
PHP Code: [ Select ]

\$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
\$white = imagecolorallocate(\$im, 255, 255, 255);
for (\$y = 0; \$y < 512; \$y++) {
for (\$x = 0; \$x < 512; \$x++) {
if (mt_rand(0, 1)) {
imagesetpixel(\$im, \$x, \$y, \$white);
}
}
}

imagepng(\$im);
imagedestroy(\$im);

2.
3. \$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
4. \$white = imagecolorallocate(\$im, 255, 255, 255);
5. for (\$y = 0; \$y < 512; \$y++) {
6.     for (\$x = 0; \$x < 512; \$x++) {
7.         if (mt_rand(0, 1)) {
8.             imagesetpixel(\$im, \$x, \$y, \$white);
9.         }
10.     }
11. }
12.
13. imagepng(\$im);
14. imagedestroy(\$im);
15.

I say its pretty random http://www.indefinitedesigns.com/randtest2.png

PHP mt_rand finding even or odd numbers
PHP Code: [ Select ]

\$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
\$white = imagecolorallocate(\$im, 255, 255, 255);
for (\$y = 0; \$y < 512; \$y++) {
for (\$x = 0; \$x < 512; \$x++) {
\$rand = mt_rand(1, 512);
if (\$rand % 2) {
imagesetpixel(\$im, \$x, \$y, \$white);
}
}
}

imagepng(\$im);
imagedestroy(\$im);

2.
3. \$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
4. \$white = imagecolorallocate(\$im, 255, 255, 255);
5. for (\$y = 0; \$y < 512; \$y++) {
6.     for (\$x = 0; \$x < 512; \$x++) {
7.         \$rand = mt_rand(1, 512);
8.         if (\$rand % 2) {
9.             imagesetpixel(\$im, \$x, \$y, \$white);
10.         }
11.     }
12. }
13.
14. imagepng(\$im);
15. imagedestroy(\$im);
16.

I say its pretty random http://www.indefinitedesigns.com/randtest3.png

I don't see much difference in the two mt_rand examples
• Posts: 131

3+ Months Ago

Ha awesome. Thanks for adding my suggestion. It's pretty interesting. I think the randomness is fairly legit. I got up to 17 times in a row. Which is about a 1 in 130k chance I think. I know it's just a time burner, but they are good exorcise and it's cool. Good job.

Edit: And working out the math is my favorite part of coding.

## Post Information

• Total Posts in this topic: 6 posts
• Users browsing this forum: spork and 65 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum