note 91578 added to function.rand
| From: | misterfright at nospamgmail dot com | Date: | Wed, 17 Jun 2009 14:40:35 +0000 |
| Subject: | note 91578 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156989@lists.php.net to get a copy of this message | ||
alex, I've seen your method in a classic textbook as what NOT to do.
Imagine you're trying to get a random array of playing cards (i.e. shuffle). What you would do
with your method would be like taking one of the 52 cards out of the deck, checking if its the list
that you are making. If its not, you write it down in your list. If it is, you draw another card
and try again.
Now imagine if your list has 51 cards, and you're trying to get that last one, say it happens
to be three of clubs. You would keep drawing and saying "Is this the three of clubs? No",
put it back, "Is this the three of clubs? No", put it back...
With a very large set to draw from, this could take awhile.
Something better would be to make a list of things to draw from. When something is randomly taken
out, remove from that list. So you would have list[0 thru 51], each entry would have a string
saying "three of clubs" or something. When picked, you would remove it, then would be
left with list[0 thru 50]. Until you would be left with list[only with 0].
----
Server IP: 209.41.74.194
Probable Submitter: 192.91.173.42
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/91578
Del: integrated -- https://master.php.net/note/delete/91578/integrated
Del: useless -- https://master.php.net/note/delete/91578/useless
Del: bad code -- https://master.php.net/note/delete/91578/bad+code
Del: spam -- https://master.php.net/note/delete/91578/spam
Del: non-english -- https://master.php.net/note/delete/91578/non-english
Del: in docs -- https://master.php.net/note/delete/91578/in+docs
Del: other reasons-- https://master.php.net/note/delete/91578
Reject -- https://master.php.net/note/reject/91578
Search -- https://master.php.net/manage/user-notes.php