php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18557 array_rand() with mt_srand() not giving me random values
Submitted: 2002-07-25 03:12 UTC Modified: 2002-07-25 08:50 UTC
From: naintara at ndf dot vsnl dot net dot in Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.2.1 OS: Windows 2000 Professional Server
Private report: No CVE-ID: None
 [2002-07-25 03:12 UTC] naintara at ndf dot vsnl dot net dot in
I have an array of 26 characters. I want three random values from the array, I use the following:
$rand_al = array_rand ($arralpha, 3);

Prob: I get the same characters each and every time.

What I am doing: First I seed the generator with
mt_srand ((double) microtime() * 1000000);

I fill my array $arralpha with values, and use the array_rand() function to return me random indexes
$rand_al = array_rand ($arralpha, 3);

On echoing the values at those indexes, I got the same values each time

Workaround: The way I am handling it right now is generating random values using mt_rand(0,25) 
and using those random values as the array subscripts. This method gives me random values.

Additional Info: mt_getrandmax() returned 2147483647.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-25 04:41 UTC] sniper@php.net
You should be calling srand() NOT mt_srand()..does this make any difference? (actually it should not be even necessary anymore to call that either since 4.2.0..)

 [2002-07-25 05:06 UTC] naintara at ndf dot vsnl dot net dot in
I tried with srand() too, it made no difference.

In fact, even after a full day, I am getting the same value I was getting yesterday.

I'd like to add from my observation, the random values returned through rand() value is not the problem, the problem is with the array_rand() values returned.
 [2002-07-25 05:17 UTC] edink@php.net
This should be fixed in this snapshot:

http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

Could please see if this fixes your problem?
 [2002-07-25 08:49 UTC] naintara at ndf dot vsnl dot net dot in
After installing the following:
http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

PHP 4.2.3-dev version the bug seems to be fixed. 
Thanks so much for your time.
 [2002-07-25 08:50 UTC] derick@php.net
Closing then
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 21:01:29 2024 UTC