user3454805 user3454805 - 2 months ago 7
PHP Question

generate 30 random numbers between 1 and 10 using php

//`enter code here

function randomNumbers ($start,$end,$quantity){
$numbers = range($start, $end);
shuffle($numbers);

return array_slice($numbers,0,$quantity);
}
$array=(randomNumbers(1,10,30));//calling a function
print_r($array);// printing the output


//problem with i am only getting 10 numbers not 30 as i want

Answer

You are getting only 10 numbers with this line

$numbers = range($start, $end);  // Get 1..10 range

so array_slice can get only 10 numbers. You can try with someting like this (maybe it is not the best code, but can help you)

function randomNumbers ($start,$end,$quantity){
    $numbers = array();
    for ($i=0; $i<$quantity/($end-$start); $i++)
        $numbers = array_merge($numbers, range($start, $end));
    shuffle($numbers);

    return array_slice($numbers,0,$quantity);
}