note 91578 deleted from function.rand by danbrown

From: Date: Wed, 17 Jun 2009 14:41:04 +0000
Subject: note 91578 deleted from function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156990@lists.php.net to get a copy of this message
Note Submitter: misterfright at nospamgmail dot com ---- 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].

« previous php.notes (#156990) next »