note 59065 deleted from control-structures.for by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Dec 2009 16:05:23 +0000 |
| Subject: | note 59065 deleted from control-structures.for by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163651@lists.php.net to get a copy of this message | ||
Note Submitter: jdtoth -at- gmail dot com
----
I was fooling around, trying to figure out the combination to a lock that I couldnt remember the
combo to. I did remember it involved a few numbers, so I wrote the following:
<?php
// ----------------------------------------------------------------------
// output all possible combinations for lock, based on supplied numbers
// ----------------------------------------------------------------------
// suspected combination numbers
$numbers = array (12,18,24,36);
for ($i=0; $i <= 10000; $i++)
{
srand ((double) microtime() * 1000000);
$random1 = rand(0,3);
srand ((double) microtime() * 1000000);
$random2 = rand(0,3);
srand ((double) microtime() * 1000000);
$random3 = rand(0,3);
$results[] = $numbers[$random1].' - '.$numbers[$random2].' -
'.$numbers[$random3];
}
// discard duplicates
$results = array_unique($results);
echo "** Unique Results ".count($results)." ** <br/><br/>";
foreach ($results as $output) echo $output . "<br />";
?>
What would be the proper way of writing this code? Obviously my 'brute-force' method of
running a random sequence an extreme amount of times does work and yields what i think is the
correct number of combinations, but there must be a more efficient method?