php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28305 range problem
Submitted: 2004-05-06 19:44 UTC Modified: 2004-05-06 20:14 UTC
From: anakin at php dot pl Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.3.4 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: anakin at php dot pl
New email:
PHP Version: OS:

 

 [2004-05-06 19:44 UTC] anakin at php dot pl
Description:
------------
Using array_rand wirh range function produces incorrect results.

Reproduce code:
---------------
<?php
 srand();
 print_r(array_rand(range(1, 6), 6));
?>

Expected result:
----------------
An array with numbers from 1 to 6 shuffled.

Actual result:
--------------
For example:
Array ( [0] => 4 [1] => 2 [2] => 3 [3] => 1 [4] => 0 [5] => 5 ) 
See key '4'. 0 (zero) does not match range(1, 6).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-06 20:14 UTC] pollita@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

From the manual:

If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array. 

Your "actual results" are what you should expect, arrray KEYS 0 through 5.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC