Reputation: 5
The following code generates random number from 1 to 10
$ran = rand(1,10);
What if i want random numbers from my custom values.that is (1,3,6,7,9)
for example i need to generate any one number from the group (1,3,6,7,9)
Upvotes: 0
Views: 307
Reputation: 2314
You want array_rand()
or you could shuffle()
and always just reference $array[0]
which would also be "random"
Upvotes: 1
Reputation: 4239
You need array_rand
function for that . the Parameters are array_rand ( array $input [, int $num_req = 1 ] )
. For your purpose , use it as $var_rand = array_rand($arr);
as you want only 1 random number generated . For more help, refer to this
Also , for future questions , post your php version too. Some functions are not available in older versions . This one is available , though.
Upvotes: 0
Reputation: 2640
You can do that as following,
<?PHP
$numbers = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6);
$random_key = array_rand($numbers, 1);
print $random_key;
?>
Upvotes: 1