Jump to content

Returning random values from an array


talk2toyin

Recommended Posts

Create a function called getRandomLetter() or something, which:

 

1. Generates a random number, rand($min, $max).

 

2. Returns the element in the array with the generated number, like: return $mixedL[$randomizednumber]

 

Then call this function every time you need a new number.

 

- W


$mixedL = array_merge(range('A','Z'), range('a', 'z'));

for ($i=0; $i<10; $i++)
{
    shuffle($mixedL);
    echo join(array_slice($mixedL,0,4)) .'<br>';
}

/*** results
OPTq
HSoh
awfI
CNKj
fjHG
Pmht
ZOWs
PzoJ
pqYz
FnfM
*/

or

 

$mixedL = array_merge(range('A','Z'), range('a', 'z'));
$x = array_rand($mixedL, 4);
foreach ($x as $ind) {
    echo $mixedL[$ind];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.