数组 函数
在线手册:中文 英文
PHP手册

array_rand

(PHP 4, PHP 5)

array_rand 从数组中随机取出一个或多个单元

说明

mixed array_rand ( array $input [, int $num_req ] )

array_rand() 在你想从数组中取出一个或多个随机的单元时相当有用。它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1。

如果你只取出一个,array_rand() 返回一个随机单元的键名,否则就返回一个包含随机键名的数组。这样你就可以随机从数组中取出键名和值。

Example #1 array_rand() 例子

<?php
srand
((float) microtime() * 10000000);
$input = array("Neo""Morpheus""Trinity""Cypher""Tank");
$rand_keys array_rand($input2);
print 
$input[$rand_keys[0]] . "\n";
print 
$input[$rand_keys[1]] . "\n";
?>

参见 shuffle()

参数

input

The input array.

num_req

Specifies how many entries you want to pick. Trying to pick more elements than there are in the array will result in an E_WARNING level error.

返回值

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.

更新日志

版本 说明
5.2.10 The resulting array of keys is no longer shuffled.
4.2.0随机数发生器自动进行播种。

范例

Example #2 array_rand() example

<?php
$input 
= array("Neo""Morpheus""Trinity""Cypher""Tank");
$rand_keys array_rand($input2);
echo 
$input[$rand_keys[0]] . "\n";
echo 
$input[$rand_keys[1]] . "\n";
?>

参见


数组 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 从数组中随机取出一个或多个单元

用户评论:

qeremy (07-Mar-2012 09:18)

An example for getting random value from arrays;

<?php
function array_random($arr, $num = 1) {
   
shuffle($arr);
   
   
$r = array();
    for (
$i = 0; $i < $num; $i++) {
       
$r[] = $arr[$i];
    }
    return
$num == 1 ? $r[0] : $r;
}

$a = array("apple", "banana", "cherry");
print_r(array_random($a));
print_r(array_random($a, 2));
?>

cherry
Array
(
    [0] => banana
    [1] => apple
)

And example for getting random value from assoc arrays;

<?php
function array_random_assoc($arr, $num = 1) {
   
$keys = array_keys($arr);
   
shuffle($keys);
   
   
$r = array();
    for (
$i = 0; $i < $num; $i++) {
       
$r[$keys[$i]] = $arr[$keys[$i]];
    }
    return
$r;
}

$a = array("a" => "apple", "b" => "banana", "c" => "cherry");
print_r(array_random_assoc($a));
print_r(array_random_assoc($a, 2));
?>

Array
(
    [c] => cherry
)
Array
(
    [a] => apple
    [b] => banana
)

Sebmil (05-Aug-2011 08:18)

Looks like this function has a strange randomness.

If you take any number of elements in an array which has 40..100 elements, the 31st one is always by far the less occuring (by about 10% less than others).

I tried this piece of code at home (PHP Version 5.3.2-1ubuntu4.9) and on my server (PHP Version 5.2.17), unfortunately i haven't any server with the last version here :

<?php
$valeurs
= range(1, 40);
$proba = array_fill(1, 40, 0);
for (
$i = 0; $i < 10000; ++$i)
{
   
$tirage_tab = array_rand($valeurs, 10);
    foreach(
$tirage_tab as $key => $value)
    {
       
$proba[$valeurs[$value]]++;
    }
}

asort($proba);
echo
"Proba : <br/>\n";
foreach(
$proba as $key => $value)
{
    echo
"$key : $value<br/>\n";
}
?>

In every try, the number of occurrences change a bit but the 31 is always far less (around 2200) than the others (2400-2600). I tried with 50 and 100 elements, no change. I tried with more or less elements to pick (second parameter to array_rand), same result. If you pick only one element it's even worse : 31 has half the result of the others.

For this particular case, i recommend shuffling the array and taking the nth first elements, in this test it's 60% faster and the statistics are ok.

zenko (15-Jun-2010 07:55)

unset() seem to me to be a better way to have a unique key in the generated result.
Taking 1 from $i can last very long if you generate a long password (try with 20 chars) because array_rand can always "choose" a key which has already been used.
<?php

$my_array
= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5");
$parola = '';
    for (
$i=0; $i<=15; $i++)
    {
       
// Generates the random number from the array
       
$random = array_rand($my_array);
       
       
// Echo the count number and the random number called from array_rand
        // and print the array to see the last random char has been deleted
       
echo '<br /> count ' . $i . ' => ' . $random . ' call<br />';
       
print_r($my_array);
       
       
// Add the char to parola...
       
$parola .= $my_array[$random];
       
       
// ...then unset the char from the array
       
unset($my_array[$random]);
       
    }
   
   
// Echo the result
   
echo '<br /><br /> Parola generated : ' . $parola;
?>

Anonymous (02-Oct-2009 09:04)

If the array elements are unique, and are all integers or strings, here is a simple way to pick $n random *values* (not keys) from an array $array:

<?php array_rand(array_flip($array), $n); ?>

darren(at)ironworks-crawley(do)co(do)uk (24-Feb-2009 03:52)

adapted from herodesh's code,
will pull a number of unique from arrays.

<?php

$my_array
= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5");
        for (
$i=0; $i<=10; $i++)
        {
           
$random = array_rand($my_array);
                       
//this generates the random number from the array
           
echo "<br> count $i <br> $random call";           
                   
// echo the count number and the random number called from array_rand
                   
if ($check[$random]<>0) {
                   
$i--;                   
                    echo
"ed already try again";
                   
// if check array isnt = 0 then the random number has been already called re random by taking 1 from $i to ensure correct number of results
                   
} else {
                   
$check[$random]=1;
                   
// set check to 1 so it knows the array number has been used
           
$parola .= $my_array[$random];}
                       
//here we will display the exact charachter from the array
       
}
        echo
"<br> $parola"; // printing result
       
?>
//delete as required
This is my first php script or code or adaption (so be kind),As its most likely bad coding. i have been looking at using php to pull and display random pictures to display on the website not pulling the same picture twice. work obviously required.changing contents of $my_array to "<img src="bla-bla1.jpg>"
Basically it just calls an array_rand as in herodesh's code, but uses a second "fake" array to flag true or false (1 or null) to compare if it has been called prior. changing $check[$random]=1 to $check[$random]++1 and if ($check[$random]<>0) to if ($check[$random]<2) obviously changes the number of times it allows the same array to be called randomly.

info at pavliga dot com (28-Dec-2008 09:52)

If you want get unique range:

<?php
$n
= 15;

$data = range(1, 20);
$rand = array_rand($data,$n);

for(
$i=0; $i<$n; $i++)
{
echo
$rand[$i]."<br>";
}

?>

admin at jeremyzaretski dot com (15-Dec-2008 04:11)

<?php
// a foreach friendly version of array_rand
function Select_Random_Indices($source_array, $count = 1)
{
    if(
$count > 0)
    {
        if(
$count == 1)
        {
           
$result = array(array_rand($source_array, $count));
        }
        else
        {
           
$result = array_rand($source_array, $count);
        }
    }
    else
    {
       
$result = array();
    }

    return
$result;
}

// using the above function to pick random values instead of entries
function Select_Random_Entries($source_array, $count = 1)
{
   
$result = array();
   
$index_array = Select_Random_Indices($source_array, $count);

    foreach(
$index_array as $index)
    {
       
$result[$index] = $source_array[$index];
    }

    return
$result;
}
?>

MadeGlobal (30-May-2008 11:16)

A problem exists with the array_pick function suggested by adrian.quark - if the function only has to remove one item from the array (e.g. original array has five entries and you want it to return four), you get a problem with the different behavior of array_rand when the parameter is 1

Fixed implementation:

<?php

function array_pick($hash, $num) {
 
$count = count($hash);
  if (
$num <= 0) return array();
  if (
$num >= $count) return $hash;
 
$required = $count - $num;
  if (
$required == 1) {   //array rand returns the KEY if there is only one item requested so...
     
$keys = array(array_rand($hash, $required));
  } else
     
$keys = array_rand($hash, $required);
  foreach (
$keys as $k) unset($hash[$k]);                   
  return
$hash;
}

?>

adrian.quark at alchemysystems dot com (04-Jan-2008 06:57)

Here is a function to pick a random fixed-size subset of an array without changing the order. It's possible to be slightly faster in some cases if you know the array keys are numeric or if you're only picking a few elements, but this is a good general implementation.

<?php
/**
 * Pick $num elements from $array at random, preserving order and keys.
 */
function array_pick($hash, $num) {
       
$count = count($hash);
        if (
$num <= 0) return array();
        if (
$num >= $count) return $hash;
       
$keys = array_rand($hash, $count - $num);
        foreach (
$keys as $k) unset($hash[$k]);
        return
$hash;
}
?>

herodesh [-at_] gmail [-dot-] com (05-Sep-2007 11:36)

this is to generate a random selection from an array with array_rand preety nice, can be used to generate random passwords or anything:

$my_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5");
        for ($i=0; $i<=10; $i++)
        {
            $random = array_rand($my_array);
                        //this generates the random number from the array
            $parola .= $my_array[$random];
                        //here we will display the exact charachter from the array
        }
        echo $parola; // printing result

Hayley Watson (12-Jun-2007 04:38)

To add to asp at cyberlin dot eu's comment. Not only will it return NULL but it will also trigger a warning, and not only if the number of elements requested is too large to be satisfied by the array but also if it is zero. The warning message sums the situation up: "Second argument has to be between 1 and the number of elements [inclusive] in the array".

asp at cyberlin dot eu (13-Apr-2007 09:24)

array_rand() is very useful, but note:

In the case that the count of the array is smaller then the optional parameter 'num_req', it will return an empty array (or string).

Reinoud Elhorst (10-Apr-2007 11:50)

Please note that (at least in PHP 4.3.11), while each key from the source array has a equal chance of being picked, the order in which they are returned is NOT random. In an array with keys from one to 10, where 2 random keys are picked, 0 is picked as the first number 18.8% of the time, and only as second number in 1.1% of the time:

$n=100000;
$a=array(1,2,3,4,5,6,7,8,9,10);
$b=array();
for($i=0;$i<sizeof($a);$i++) {
  $b[$i]=array(0,0);
}
for($i=0;$i<$n;$i++) {
  $keys=array_rand($a,2);
  $b[$keys[0]][0]++;
  $b[$keys[1]][1]++;
}
for ($i=0;$i<sizeof($b);$i++){
  printf("%d: %04.1f%% %04.1f%% %04.1f%%", $i, ($b[$i][0]/$n*100),($b[$i][1]/$n*100),($b[$i][0]+$b[$i][1])/$n*100);
}

The result is:
0: 18.8% 01.1% 19.9%
1: 17.0% 03.3% 20.3%
2: 14.3% 05.7% 20.0%
3: 12.2% 07.9% 20.1%
4: 10.0% 10.0% 20.0%
5: 07.8% 12.0% 19.8%
6: 05.5% 14.5% 20.0%
7: 03.3% 16.6% 19.9%
8: 01.2% 18.9% 20.1%
9: 10.0% 09.9% 19.9%

The workaround is adding a shuffle command to shuffle the keys:

0: 10.0% 10.0% 20.0%
1: 10.0% 10.0% 20.0%
2: 10.0% 10.0% 20.0%
3: 10.0% 10.0% 20.0%
4: 10.0% 10.0% 20.0%
5: 10.0% 10.0% 20.0%
6: 10.1% 10.0% 20.1%
7: 10.0% 10.0% 20.0%
8: 10.0% 10.0% 20.0%
9: 10.0% 10.0% 20.0%

Sven Arduwie (29-Jan-2007 01:40)

Here is my array_mt_rand function.
<?php
   
function array_mt_rand(array $array, $numberOfKeys = 1)
    {
        if (!
is_int($numberOfKeys)) throw new Exception;
        if (
$numberOfKeys < 1) throw new Exception;
       
$keys = array_keys($array);
       
$maximum = count($array) - 1;
        if (
$numberOfKeys == 1) {
            return
$keys[mt_rand(0, $maximum)];
        } else {
           
$randomKeys = array();
            for (
$i = 0; $i < $numberOfKeys; $i++) {
               
$randomKeys[] = $keys[mt_rand(0, $maximum)];
            }
            return
$randomKeys;
        }
    }
?>

Frederick Lemasson aka djassper at gmail (04-Dec-2006 08:57)

To select a random Value (not a Key) from a Multi-Dimentionnal array I made a recursive function : array_multi_rand()

the following exemple randomly selects an url from a multidimentionnal array :

<?

$Expos['Google']['Science']='news.google.fr/news?topic=t';
$Expos['Google']['Economie']='news.google.fr/news?topic=b';
$Expos['Google']['Sante']='news.google.fr/news?topic=m';
$Expos['Yahoo']='fr.news.yahoo.com';
$Expos['Events']['LogicielLibre']='agendadulibre.org';
$Expos['MyBlog']='www.kik-it.com';

function array_multi_rand($Zoo){
    $Boo=array_rand($Zoo);
    if(is_array($Zoo[$Boo])){
        return array_multi_rand($Zoo[$Boo]);
    }else{
        return $Zoo[$Boo];
    }
}

echo(array_multi_rand($Expos));

?>

jan at planetromeo dot com (24-Nov-2006 04:56)

Instead of using the horrible slow

select * from some_table order by rand() limit 1;

have a look at this more performant solution from
http://jan.kneschke.de/projects/mysql/order-by-rand/

SELECT * FROM some_table AS r1
JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM some_table)) AS id) AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1;

alec (11-Nov-2006 01:37)

select * from some_table order by rand() limit 1;

is extremely slow for large tables. large as in a a few thousand entries.

dbrooks at icarusstudios dot com (05-Sep-2006 10:28)

Instead of looping through all the rows selected to pick a random one, let the server do the work for you:

select * from some_table order by rand() limit 1;

JS (04-Sep-2006 11:07)

I wanted to write something that picks a random entry from a 1column-MySQL database - simply Post Of The Moment (potm). I know there surly are many better ways to do it, but I`m rather new to PHP :)  Anyway, it`s simple and no-problem working code.
Of course I assume your DB exists and you always have something in it.

@$link = MySQL_Connect("localhost", "username", "password"); //connect to mysql
mySQL_Select_DB("database"); //..to DB
@$potms = MySQL_Query("SELECT * FROM potm"); //now we get all from our table and store it
MySQL_Close($link); //there`s no need for connection, so we should close it

$potm_array = ''; //sets variables to "zero" values
$i = 0;
while ($entry = MySQL_Fetch_Array($potms)) //now we go through our DB
       {
         $potm_array[$i] = $entry; //our temporary array from which we will random pick a field key
         $i++; //now we increment our field key
       }

$potm_id = array_rand($potw_array); //picks a random key from array
$potm = $potm_array[$potm_id]['name_of_the_field']; //now we have stored our Post Of The Moment in $potm

..hope this helps

mark at rugbyweb dot org (30-Aug-2006 09:29)

I was setting up a rotating add banner and was trying to find a way to go about it using an auto_increment id from the database, so I wrote this little script:

<?php
require "mysql.php";
global
$c;
print
"<center>";
$qq=mysql_query("SELECT adID FROM ads WHERE adCLICKS<=adMAX",$c) or die (mysql_error()."<br>Error, please report this error quoting #QAB");
while(
$q=mysql_fetch_array($qq))
{
$ad1[$q['adID']]++; //Used an array and inserted the database query results into it.
}
$ad=array_rand($ad1, 1); //Did a random array function and then used it in the query below.
$er=mysql_query("SELECT * FROM ads WHERE adID='$ad'") or die (mysql_error()."<br>Error, please report this error quoting #QAB3");
if(!
mysql_num_rows($er))
{
print
"<A href='advertise.php'>Place an ad here!</a>";
exit;
}
$r=mysql_fetch_array($er);
print
"<a href='adsout.php?ID={$r['adID']}&BLOCK=1'><img src='{$r['adAD']}' alt='{$r['adNAME']}' height='60' width='468'></a>";
?>

dragonfly at dragonflyeye dot net (07-Jul-2006 09:28)

Well, I came up with a workaround for my earlier problem.  For those of you who might need to shuffle around multi-dimensional arrays (I used this to randomize a list of headlines from a MySQL database query), here's what I did to get the desired output:

$subs = //my query went here
$shfl = array_rand($subs, 5);
$shsubs = array();
for ($x=0; $x<count($shfl); $x++) {
    $shsubs[] = $subs[$shfl[$x]];
}

There may in fact be a much easier way to do this, but this is what I came up with and its working!

Thanks to everyone that makes php.net the invaluable tool for us nooobies!

dragonfly at dragonflyeye dot net (07-Jul-2006 03:57)

Well, this is interesting.   I don't see anyone else commenting on this, so just in case you were planning to use this function like I was, be prepared: array_rand does not handle multidimensional arrays.  It just ends up returning a list of the X-axis values without the Y-axis arrays.  Bummer.  I'm going to have to find another way to do what I wanted.

steve at webcommons dot biz (05-Jul-2006 10:12)

Following are functions for getting a random key or value (or array of random keys or values) from a filtered array.  In other words, given an array of data, these functions filter out what you don't want, then extract a random element or bunch from what's left.

$CallBack is the filtering function. 

  // Utility function for functions below
  function GetRandomArrayValue($TheArray) {
      return $TheArray[array_rand($TheArray)];
  }
 
 
  // Get a random key or array of keys from a filtered array
  function GetRandomFilteredArrayKeys($aInput, $CallBack = NULL, $NumReq = 1) {
      $FilteredKeys = array_keys(array_filter($aInput, $CallBack));
     
      if ($NumReq <= 1) {
        return GetRandomArrayValue($FilteredKeys);
      }
      else {
        shuffle($FilteredKeys);
       
        return array_slice($FilteredKeys, 0, $NumReq);
      }
  }
 
  // Get a random value or array of values from a filtered array
  function GetRandomFilteredArrayValues($aInput, $CallBack = NULL, $NumReq = 1) {
      $aFiltered = array_filter($aInput, $CallBack);
     
      if ($NumReq <= 1) {     
        return $aFiltered[GetRandomArrayValue(array_keys($aFiltered))];
      }
      else {
        shuffle($aFiltered);
       
        return array_slice($aFiltered, 0, $NumReq);
      }
  }

trukin at gmail dot com (16-Mar-2006 09:23)

Modify of last note:
<?php
if (!function_exists('array_rand')) {
    function
array_rand($array, $lim=1) {
       
mt_srand((double) microtime() * 1000000);
        for(
$a=0; $a<=$lim; $a++){
           
$num[] = mt_srand(0, count($array)-1);
        }
        return @
$num;
    }
}
?>

mt_rand generates a better random number, and with the limit.

emailfire at gmail dot com (13-Mar-2006 09:32)

<?php
if (!function_exists('array_rand')) {
function
array_rand($array) {
srand((double) microtime() * 1000000);
return
rand(0, count($array)-1);
}
}
?>

Will-ster (02-Jan-2006 12:18)

This is something I have been playing with for quite awhile. I'm very new to php, but i finally got it to work. it's a function that will take and array[$arrquo] and find a particular keyword[$find] in the different elements of the array then take those elements that posess that keyword and display them at random

<?php
function popbyword($arrquo,$find)
{
$newarr = array('');
 foreach(
$arrquo as $line)
 {
  if(
strstr( $line, $find ) )
  {
   
array_push($newarr, $line);
 
  }
 }   
srand((double)microtime()*1000000);
$rquote = array_rand($newarr);
echo
$newarr[$rquote];
}

popbyword($images, 'Albert');
?>

In my case I had this huge array of quotes with 90 some elements. I was able to find certain keywords in those elements then ONLY display the elements that had those keywords. NEAT! Maybe only because I'm new.

farooqym at ieee dot org (01-Jan-2006 10:05)

Here's an algorithm to make a weighted selection of an item from an array.
Say we have an array $items with keys as items and values as corresponding weights.
For example:
<?php
$items
= array(
   
item1 => 3,
   
item2 => 4,
   
item3 => 5,
);
?>
i.e. we want to choose item1 25% of the time, item2 33.3% of the time and item3 41.6% of the time.
Here's a function that works when the weights are positive integers:
<?php
function array_rand_weighted($values) {
   
$r = mt_rand(1, array_sum($values));
    foreach (
$values as $item => $weight) {
        if  (
$r <= $weight) return $item;
       
$r -= $weight;
    }
}
?>
Enjoy!

bjcffnet at gmail dot com (30-Aug-2005 12:29)

As wazaawazaa600 at msn dot com pointed out, a multi-dimensional array doesn't work with this function. So, I hope I can help someone with this :)

<?php
/**
 * Returns a number of random elements from an array.
 *
 * It returns the number (specified in $limit) of elements from
 * $array. The elements are returned in a random order, exactly
 * as it was passed to the function. (So, it's safe for multi-
 * dimensional arrays, aswell as array's where you need to keep
 * the keys)
 *
 * @author Brendan Caffrey  <bjcffnet at gmail dot com>
 * @param  array  $array  The array to return the elements from
 * @param  int    $limit  The number of elements to return from
 *                            the array
 * @return array  The randomized array
 */
function array_rand_keys($array, $limit = 1) {
   
$count = @count($array)-1;

   
// Sanity checks
   
if ($limit == 0 || !is_array($array) || $limit > $count) return array();
    if (
$count == 1) return $array;

   
// Loop through and get the random numbers
   
for ($x = 0; $x < $limit; $x++) {
       
$rand = rand(0, $count);

       
// Can't have double randoms, right?
       
while (isset($rands[$rand])) $rand = rand(0, $count);

       
$rands[$rand] = $rand;
    }

   
$return = array();
   
$curr = current($rands);

   
// I think it's better to return the elements in a random
    // order, which is why I'm not just using a foreach loop to
    // loop through the random numbers
   
while (count($return) != $limit) {
       
$cur = 0;

        foreach (
$array as $key => $val) {
            if (
$cur == $curr) {
               
$return[$key] = $val;

               
// Next...
               
$curr = next($rands);
                continue
2;
            } else {
               
$cur++;
            }
        }
    }

    return
$return;
}
?>

alexkropivko at(dog) yandex dot ru (16-Jun-2005 02:21)

There was a mistake at "Paul Hodel (paul at ue dot com dot br) 17-Apr-2003 04:40":
String
echo $new_input = $input[$v];

have to be:
echo $new_input[] = $input[$v];

maxnamara at yahoo dot com (13-Mar-2005 11:22)

<?php
$input
= array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

function
my_array_rand($input,$i=2){
srand((float) microtime() * 10000000);

$rand_keys = array_rand($input, $i);

/*
print $input[$rand_keys[0]] . "\n";
print $input[$rand_keys[1]] . "\n";
*/

$res = array();

if(
$i > 1){

for(
$a=0;$a<$i;$a++){

   
$res[] = $input[$rand_keys[$a]];
   
}

}
else{

   
$res[] = $input[$rand_keys];   
   
}

return
$res;
}

$a = my_array_rand($input,3);
echo
"<pre>";
print_r($a);
echo
"</pre>";
?>

yhoko at yhoko dot com (28-Aug-2004 02:16)

According to office at at universalmetropolis dot com I have to say that the example is wrong.

<?php
// retrieve one of the options at random from the array
$teamcolours = $teamcolours[rand(0,count($teamcolours))];
?>

The count() function will return the number of items in the array, that's the last index + 1. So if there's 2 items in the array, count() will return 2 but the indices are 0 and 1. Now since rand(x,y) randomizes between x and y inclusively the index from the above example may be out of bounds. Thus you have to subtract 1 from the count:

<?php
   
// Get random item
   
$teamcolours = $teamcolours[rand(0,count($teamcolours)-1)];
?>

leighm at linuxbandwagon dot com leigh morresi (09-Mar-2004 03:36)

Another array based password generator, this one is a port from the python mailman version.

this generates slightly predictable but human readable passwords that people can remember
output passwords are for example "rikanumi"

<?php
// port of mailman version
function MakeRandomPassword($length=6) {
$_vowels = array ('a', 'e', 'i', 'o', 'u');   
$_consonants = array ('b', 'c', 'd', 'f', 'g', 'h', 'k', 'm', 'n','p', 'r', 's', 't', 'v', 'w', 'x', 'z');   
$_syllables = array ();   
foreach (
$_vowels as $v) {
    foreach (
$_consonants as $c) {   
       
array_push($_syllables,"$c$v");   
       
array_push($_syllables,"$v$c");
    }
}

for (
$i=0;$i<=($length/2);$i++)         $newpass=$newpass.$_syllables[array_rand($_syllables) ];

return
$newpass;
?>

}

tim dot meader at gsfc dot nasa dot gov (06-Mar-2004 02:31)

Just thought I would contribute a password generation function
that uses array_rand. I wrote this because I
could not find anywhere a PHP equivalent of the ability
that the String::Random module in Perl has, which allows
you to specify a schema for how you want the random
string created. In other words: I want 2 Uppercase, 3
lowercase, 2 intergers...etc.  This isn't too comprehensive,
notably it doesn't account for one choosing more itterations
 of a particular type than there are in the array (ie -
choosing more than 10 numbers from output). Additionally,
this doesn't allow for any repeated characters. Hope it can
be of use... comments appreciated.

<?php
   
function &doGeneratePasswords()
    {
       
//////////////////////////////////////
        // lowercase L left out for clarity //
        //////////////////////////////////////
       
$l_achLowercase = array("a","b","c","d","e","f","g","h",
                                 
"i","j","k","m","n","o","p","q",
                                 
"r","s","t","u","v","w","x",
                                 
"y","z");
       
$l_iNumLowercase = count($l_achLowercase);

       
////////////////////////////////////////////
        // uppercase I and O left out for clarity //
        ////////////////////////////////////////////
       
$l_achUppercase = array("A","B","C","D","E","F","G","H",
                                 
"J","K","L","M","N","P","Q",
                                 
"R","S","T","U","V","W",
                                 
"X","Y","Z");
       
$l_iNumUppercase = count($l_achUppercase);

       
$l_aiNumbers = array("1","2","3","4","5","6","7","8","9","0");
       
$l_iNumNumbers = count($l_aiNumbers);

       
$l_achSpecialChars = array("!","#","%","@","*","&");
       
$l_iNumSpecialChars = count($l_achSpecialChars);

       
///////////////////////////////////////////////////////////////////
        // Make sure to create enough blank spaces as you want passwords //
        ///////////////////////////////////////////////////////////////////
       
$l_astPasswds = array("","","","");

       
//////////////////////////////////////////
        // Hopefully these are self explanatory //
        //////////////////////////////////////////
       
$l_astPasswdSchemes = array("SLUUSLNN","LSUNLLNU","NNUSLLSN","LNLSUNLU");

       
$l_iNumPasswds = count($l_astPasswdSchemes);

        for (
$i=0; $i < $l_iNumPasswds; $i++) {
           
$l_iSchemeLength = strlen($l_astPasswdSchemes[$i]);

           
$l_achRandLowercase = array_values(array_rand($l_achLowercase, $l_iNumLowercase));
           
$l_achRandUppercase = array_values(array_rand($l_achUppercase, $l_iNumUppercase));
           
$l_aiRandNumbers = array_values(array_rand($l_aiNumbers, $l_iNumNumbers));
           
$l_achRandSpecialChars = array_values(array_rand($l_achSpecialChars, $l_iNumSpecialChars));

            for (
$j=0; $j < $l_iSchemeLength; $j++) {
               
$l_chCurrentOne = $l_astPasswdSchemes[$i]{$j};

                switch (
$l_chCurrentOne) {
                    case
"L":
                       
$l_astPasswds[$i] .= $l_achLowercase[array_shift($l_achRandLowercase)];
                        break;

                    case
"U":
                       
$l_astPasswds[$i] .= $l_achUppercase[array_shift($l_achRandUppercase)];
                        break;

                    case
"N":
                       
$l_astPasswds[$i] .= $l_aiNumbers[array_shift($l_aiRandNumbers)];
                        break;

                    case
"S":
                       
$l_astPasswds[$i] .= $l_achSpecialChars[array_shift($l_achRandSpecialChars)];
                        break;

                    default:
                        break;
                }
            }
        }

        return
$l_astPasswds;
    }
?>

jpinedo (02-Nov-2003 09:15)

An array of arrays example:

<?php
$banners
[0]['imagen']="imagen0.gif";
$banners[0]['url']="www.nosenada.tal";

$banners[1]['imagen']="imagen1.gif";
$banners[1]['url']="www.nose.tal";

$banners[2]['imagen']="imagen2.gif";
$banners[2]['url']="pagina.html";

$banners[3]['imagen']="imagen3.jpg";
$banners[3]['url']="../pagina.php";

$id_banner = array_rand($banners);

echo 
"Archivo:--".$banners[$id_banner]['imagen']. "<br />\n";
echo 
"URL:-----".$banners[$id_banner]['url']. "<br />\n";
?>

wazaawazaa600 at msn dot com (15-Oct-2003 08:24)

The function go well ever that you work with a simple array. An array of arrays (also called a table), not works with the function correctly. Example:

<?php
$a
=array(array(0,1),array(1,2),array(2,3),array(3,4),array(4,5));
echo
$a[0][0];echo"<br>";
echo
$a[1][0];echo"<br>";

$b=array_rand($a,2);
echo
$b[0][0];echo"<br>"; //This writes nothing
echo $b[1][0];echo"<br>"; //This writes nothing
?>

If you are in this situation, you will need to make your own solution.

asarnoNOSPAM@interbaun DOT com (24-Jun-2003 07:06)

It is correct that using array_rand() with num_req=1 will return an integer and not an array, but why get so complicated with getting just the one value.  The K.I.S.S. method would suggest to do it this way:

<?
srand((double)microtime() * 10000000);
$originalArray = array("red", "blue", "green", "brown",
"cyan", "magenta", "purle", "cheezy");
$pickOne = array_rand($originalArray, 1);
$aRandomSelection = $originalArray[$pickOne ];
echo "$aRandomSelection was the random selection made";
?>

You only need to use the foreach if the num_req >=2. In those cases the array_rand() function will return an array of random elements which are a subset of the original array.  When num_req = 1, the array_rand() function returns an integer that signifies a randomly picked key of the original array.  Hope this clarifies things ... it works for me.

c at aufbix dot org (13-Jun-2003 09:31)

If you use array_rand with num_req=1, it will return an integer, and not an array as it would in all other circumstances. You can bypass that like this:

<?php
$randelts
=array_rand($feeds,$num);

for (
$j=0;$j<count($randelts);$j++) {
  if (
$num==1) {$subq[$j]=$feeds[$randelts];}
  else {
$subq[$j]=$feeds[$randelts[$i]]}
}
?>

Paul Hodel (paul at ue dot com dot br) (17-Apr-2003 09:40)

If you trying to get a randon array just use that... it's easier! And you have no repeats...

<?

srand ((float) microtime() * 10000000);

$input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

$keys = array_rand ($input, sizeof($input));

while (list($k, $v) = each($keys))
{
    echo $new_input = $input[$v];
}

?>

scandar at home dot se (13-Apr-2003 05:58)

Note that the int num_req parameter is the required number of element to randomly select. So if your array has 3 element and num_req=4 then array_rand() will not return anything since it is impossible to select 4 random elements out of an array that only contains 3 elements. Many people think that they will get 3 elements returned but that is of course not the case.

mickoz[at]parodius[dot]com (02-Dec-2002 04:28)

For those of you thinking that it does not work for num_req = 1, it is because it return a variable and not an array.  This mainly cause some problem with people using foreach.

The correct way to handle this is explained by that example:

<?php
$some_array
= array("blah","bleh","foo","lele");

$nb_value = 1;

srand ((float) microtime() * 10000000);
$rand_keys = array_rand($some_array, $nb_value);

if(!
is_array($rand_keys))
{
 
$rand_keys = array($rand_keys);
}

print_r($rand_keys); // verify here the array of keys
echo "\n<BR>";
?>

// You can then correctly use the foreach, as it require an array to work
// If you use foreach with one element, it won't work.

<?php
$random_array
= array();

foreach(
$rand_keys as $value)
{
 
array_push($random_array, $some_array[$value]);
}

print_r($random_array);
?>

dboy at jumpstation dot org (23-Jul-2002 02:58)

If you just want to pull one random element from an array, try something like this:

<?php
mt_srand
((double) microtime() * 1000000);
$myarray = array("this", "is", "a",
                
"test", "to", "see",
                
"if", "I", "can",
                
"pull", "one", "element",
                
"from", "an", "array",
                
"randomly");
$random_index = mt_rand(0, (count($myarray)-1));
?>

Then to test the randomness or what have you try a simple:

<?php
$string
= ""; // Just to kill the warning
for ($i=0; $i<count($myarray); $i++) {
    
$random_index = mt_rand(0, (count($myarray)-1));
    
$string .= "$myarray[$random_index] ";
}
$string = rtrim($string);
echo (
$string);
?>

I've gotten extremely good output from this method and would recommend it if you're just pulling one element.

josh at 3io dot com (14-Jun-2002 11:20)

I modified fake_array_rand to always only return 1 element, and did some benchmarks against calling array_rand with the second parameter as 1.  I ran 100 samples for each function for each number of elements and took the average result.  While the internal array_rand is faster for a small number of elements, it scales very poorly.

1 elements: 2.0619630813599E-05 sec. for array_rand,8.4352493286133E-05 sec. for fake_array_rand
10 elements: 2.1675825119019E-05 sec. for array_rand,8.427619934082E-05 sec. for fake_array_rand
100 elements: 2.9319524765015E-05 sec. for array_rand,8.4599256515503E-05 sec. for fake_array_rand
1000 elements: 0.0001157283782959 sec. for array_rand,8.5572004318237E-05 sec. for fake_array_rand
10000 elements: 0.0016669762134552 sec. for array_rand,8.5201263427734E-05 sec. for fake_array_rand
100000 elements: 0.015599734783173 sec. for array_rand,8.5580348968506E-05 sec. for fake_array_rand
1000000 elements: 0.18011983394623 sec. for array_rand,8.6690187454224E-05 sec. for fake_array_rand

<?php
function fake_array_rand ($array)
{
       
$count = count ($array);
       
# Help keep the number generator random :)
       
$randval and usleep ("0.$randval");

       
# Seed the random number generator
        # Generate a random number
       
srand ((double) microtime() * 10000000);
       
$randval = rand();

       
# Use the random value to 'pick' an entry from the array
        # Count the number of times that the entry is picked
       
++$index[$randval % $count];

        return
$array[$randval % $count];
}
?>

uvm at sun dot he dot net (10-Jul-2001 02:09)

If you're just trying to draw a random subset of n elements from an array, it seems more effecient to do something like this:

<?php
function draw_rand_array($array,$draws)
{
       
$lastIndex = count($array) - 1;
       
$returnArr = array();
        while(
$draws > 1)
        {
               
$rndIndex = rand(0,$lastIndex);
               
array_push($returnArr,array_splice($array,$rndIndex,1));
               
$draws--;
               
$lastIndex--;
        }

        return
$returnArr;
}
?>

No messing with indexes when you're done... you just have an array with the elements you're looking for in it.