It's not going to be a uniform distribution (and you don't specify that it needs to be).
For the most simple solution, you don't need to do scaling or loops, you can take your random 1 to 5, and then add on a random 0 to 2;
function getMoreRandom()
{
return getRandom() + getRandom() % 3;
}
A quick test to see what the distribution looks like:
$results = array_fill(1, 7, 0);
for ($i = 0; $i < 1000000; $i++) {
$results[rand(1,5) + rand(1,5) % 3]++;
}
var_dump($results);
As stated, not designed to be uniformly random.
array(7) {
[1]=>
int(39550) // 4%
[2]=>
int(120277) // 12%
[3]=>
int(200098) // 20%
[4]=>
int(199700) // 20%
[5]=>
int(200195) // 20%
[6]=>
int(160200) // 16%
[7]=>
int(79980) // 8%
}
Slightly more complicated, and a different method to @flovs (I don't like the way his loop could last forever - hey, such is randomness)
function getMoreRandom()
{
for (
$i = 0, $v = 0;
$i < 7;
$i++, $v += getRandom()
);
return $v % 7 + 1;
}
This produces a uniform distribution
array(7) {
[1]=>
int(14397)
[2]=>
int(14218)
[3]=>
int(14425)
[4]=>
int(14116)
[5]=>
int(14387)
[6]=>
int(14256)
[7]=>
int(14201)
}