note 59065 added to control-structures.for

From: Date: Fri, 25 Nov 2005 11:35:41 +0000
Subject: note 59065 added to control-structures.for
Groups: php.notes 
Request: Send a blank email to php-notes+get-99068@lists.php.net to get a copy of this message
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? ---- Manual Page -- http://www.php.net/manual/en/control-structures.for.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59065 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59065&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59065&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99068) next »