Reputation: 99
My current code generates 116 random numbers between the range of 34 and 76 and puts them into an array called $common.
for($i = 0; $i < 116; $i++){
$common[] = mt_rand(34, 76);
}
Is it possible to generate a random number from 2 groups of numbers? For instance, I want to have it pick 116 random numbers between 1-22 and 34-76.
Upvotes: 1
Views: 1230
Reputation: 79024
for($i = 0; $i < 116; $i++){
$common[] = array(mt_rand(1, 22), mt_rand(34, 76))[mt_rand(0, 1)];
}
$range = array_merge(range(1, 22), range(34, 76));
for($i = 0; $i < 116; $i++){
$common[] = $range[array_rand($range)];
}
In the second example you can also use:
$common[] = $range[mt_rand(0, count($range)-1)];
for($i = 0; $i < 116; $i++){
$common[] = mt_rand(0, 1) ? mt_rand(1, 22) : mt_rand(34, 76);
}
Upvotes: 4