Forum : How do I pick a random value from an array?
Brief description  about Online courses   join in Online courses
View Balu  Prasad 's Profile

How do I pick a random value from an array?

How do I pick a random value from an array????
Asked by Balu Prasad | Mar 17, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Balu,
If you want to choose an entry randomly from an array then you can use the array_rand function like this:
<?php
$lottery = range(1,49);
$onenumber = array_rand($lottery);
echo "One number is: $onenumber\n\n";
?>
This will print a random number between 1 and 49.
You can also use array_rand to return a certain number of random numbers.
People often like to write a little computer program to generate their weekly lottery numbers for them. Here's how to create a lottery number generator for the UK lottery that picks six random numbers between 1 and 49 in just one line of PHP code:
<?php
foreach(array_rand(range(1,49),6) as $number) echo "$number ";
?>
This will print a random selection of six numbers to the screen, for instance:
27 33 48 21 19 43
Mar 17, 2010