note 91338 added to function.array-rand

From: Date: Sat, 06 Jun 2009 21:18:22 +0000
Subject: note 91338 added to function.array-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-156420@lists.php.net to get a copy of this message
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 ---- Server IP: 212.24.129.51 Probable Submitter: 90.178.216.222 ---- Manual Page -- http://www.php.net/manual/en/function.array-rand.php Edit -- https://master.php.net/note/edit/91338 Del: integrated -- https://master.php.net/note/delete/91338/integrated Del: useless -- https://master.php.net/note/delete/91338/useless Del: bad code -- https://master.php.net/note/delete/91338/bad+code Del: spam -- https://master.php.net/note/delete/91338/spam Del: non-english -- https://master.php.net/note/delete/91338/non-english Del: in docs -- https://master.php.net/note/delete/91338/in+docs Del: other reasons-- https://master.php.net/note/delete/91338 Reject -- https://master.php.net/note/reject/91338 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156420) next »