Created
August 11, 2012 19:08
-
-
Save AntoineAugusti/3326471 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* CONFIGURATION */ | |
$host = 'localhost'; | |
$user = 'root'; | |
$password = 'root'; | |
$db_name = 'euromillions'; | |
$time_to_play = 10000; | |
/* END OF CONFIGURATION */ | |
/* If you edit something below, the sky will fall on you. Yeah, it's true. Don't try. */ | |
mysql_connect($host, $user, $password); | |
mysql_select_db($db_name); | |
function microtime_float() | |
{ | |
list($usec, $sec) = explode(" ", microtime()); | |
return ((float)$usec + (float)$sec); | |
} | |
function generateNumber() | |
{ | |
return mt_rand(1, 50); | |
} | |
function generateStar() | |
{ | |
return mt_rand(1, 11); | |
} | |
function insertDB($stats_number, $stats_star, $array_numbers, $array_stars) | |
{ | |
// Sort arrays. Elements will be arranged from lowest to highest (values). | |
sort($array_numbers); | |
sort($array_stars); | |
// Numbers. | |
for ($i=1;$i<=5;$i++) | |
{ | |
$name_var = 'nb_'.$i; | |
${$name_var} = $array_numbers[$i-1]; | |
// Stats | |
$stats_number[$array_numbers[$i-1]] += 1; | |
} | |
// Stars. | |
for ($i=1;$i<=2;$i++) | |
{ | |
$name_var = 'star_'.$i; | |
${$name_var} = $array_stars[$i-1]; | |
// Stats | |
$stats_star[$array_stars[$i-1]] += 1; | |
} | |
// Insert into the DB. | |
$query = mysql_query("INSERT INTO euromillions (nb_1, nb_2, nb_3, nb_4, nb_5, star_1, star_2) VALUES ('$nb_1', '$nb_2', '$nb_3', '$nb_4', '$nb_5', '$star_1', '$star_2')"); | |
// We need to always return arrays in order to deal with stats at the end | |
return array($stats_number, $stats_star); | |
} | |
function initialize() | |
{ | |
// Initialize arrays for stats | |
$stats_number = array(); | |
$stats_star = array(); | |
for ($i=1;$i<=50;$i++) | |
{ | |
$stats_number[$i] = 0; | |
} | |
for ($i=1;$i<=11;$i++) | |
{ | |
$stats_star[$i] = 0; | |
} | |
// We need to always return arrays in order to deal with stats at the end | |
return array($stats_number, $stats_star); | |
} | |
function play($stats_number,$stats_star) | |
{ | |
$array_numbers = array(); | |
$array_stars = array(); | |
for ($i=1;$i<=7;$i++) | |
{ | |
if ($i >= 1 AND $i <= 5) // Numbers. | |
{ | |
do | |
{ | |
$number = generateNumber(); | |
} while (in_array($number, $array_numbers)); | |
// We haven't got the number in the array, let's insert it. Array starts at 0. | |
$array_numbers[$i-1] = $number; | |
} | |
else // Stars | |
{ | |
do | |
{ | |
$star = generateStar(); | |
} while (in_array($star, $array_stars)); | |
// We haven't got the star in the array, let's insert it. Array starts at 0. | |
$array_stars[$i-6] = $star; | |
} | |
} | |
// Everything is ok, let's insert in the DB | |
$data = insertDB($stats_number,$stats_star, $array_numbers, $array_stars); | |
// We need to always return arrays in order to deal with stats at the end | |
return array($data[0], $data[1]); | |
} | |
function hardcoreGamer($time_to_play) | |
{ | |
// Initialize arrays for stats before we play | |
$data = initialize(); | |
$stats_number = $data[0]; | |
$stats_star = $data[1]; | |
if ($time_to_play < 1 OR !is_integer($time_to_play)) // Never trust user input | |
{ | |
$time_to_play = 1; | |
} | |
for ($i=1;$i<=$time_to_play;$i++) | |
{ | |
$data = play($stats_number,$stats_star); | |
$stats_number = $data[0]; | |
$stats_star = $data[1]; | |
} | |
// We need to always return arrays in order to deal with stats at the end | |
return array($stats_number, $stats_star); | |
} | |
function displayStats($stats_number, $stats_star, $time_start, $time_to_play) | |
{ | |
// Sort arrays. From highest to lowest. We need to keep keys within the array with a descending sort for values so we use the arsort function. | |
arsort($stats_number); | |
arsort($stats_star); | |
echo '<hr/>'; | |
echo '<h1>Most common numbers</h1>'; | |
print_r($stats_number); | |
echo '<hr />'; | |
echo '<h1>Most common stars</h1>'; | |
print_r($stats_star); | |
echo '<hr/>'; | |
echo '<h1>Time</h1>'; | |
$time_end = microtime_float(); | |
$time = round($time_end - $time_start, 8); | |
$nb_tests_per_second = round($time_to_play / $time); | |
echo $time_to_play.' tests executed in '.$time.'s. That is to say '.$nb_tests_per_second.' tests per second.'; | |
} | |
// Start the chrono | |
$time_start = microtime_float(); | |
// We play | |
$data = hardcoreGamer($time_to_play); | |
// We display stats | |
displayStats($data[0], $data[1], $time_start, $time_to_play); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment