note 91338 deleted from function.array-rand by danbrown

From: Date: Mon, 08 Jun 2009 12:41:13 +0000
Subject: note 91338 deleted from function.array-rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156459@lists.php.net to get a copy of this message
Note Submitter: nospam at freeproxylist dot org ---- Hi guys, I would like to tell you about malfunction of array_rand(). I know that it looks more like a bug report but it is really difficult to prove that such bug exists so it is better just to share it with others. Let's take for example simple task: we have 20.000 random sentences and we need to generate some random texts with particular length (2000 chars) in size. Of cause everyone knows the solution: you need to select random sentence from the array and merge them unless you will not get enough sized text. With array_rand() function it would look like: <?php $sentence = $this->mRandomSentences[array_rand($this->mRandomSentences)];?> If you will test you code you will not see anything strange. But try to generate 500 such texts... after a while you will see that you will get the same (exactly) texts like you have already 'randomly' generated :) Of cause I was thinking that there was some bug in my code (despite the fact it was just couple of sentences) as a software developer I have to suspect anything. So I did simple test, I have changed only one line in my code: <?php $sentence = $this->mRandomSentences[mt_rand(0, count($this->mRandomSentences))];?> you will not believe but it helped :) it have successfully generated 500 unique texts... in other words array_rand() does not work or it has some limit for array size ;) I hope that post will help somebody With regards Administrator of http://freeproxylist.org

« previous php.notes (#156459) next »