I try to create a variable who send a random value !
$random = mt_rand(5,10);
but i want the variable random with the number i choose like this:
$random = 5;10;15;20;25;30
The variable must send randomly 5 or 10 or 15 or 25 or 30 only
I try to create a variable who send a random value !
$random = mt_rand(5,10);
but i want the variable random with the number i choose like this:
$random = 5;10;15;20;25;30
The variable must send randomly 5 or 10 or 15 or 25 or 30 only
What you could do is using the mt_rand
function with values from 1 to 6 then multiply them by 5.
Example:
$random = mt_rand(1, 6) * 5;
Use array_rand
to pick random key from array then you can put it into an another.
http://php.net/manual/en/function.array-rand.php
$input = array(5,10,15,20,25);
$rand_key = array_rand($input);
$another_array[] = $input[$rand_key];
You could use and array of element and get a random item
$items = Array(5,10,15,20,...50);
$myvalue = array_rand($items);
Assuming that you already have the values you want inside an array you can simply get a random position of the array. A rand()
limited by the first element to the size of the array can solve this
<?php
$valores = [1,5,12,17,22,30,90,2,4,6]; // choose de values here
for($i=0;$i<200;$i++){ // just to print more than 1 value
echo $valores[rand(1,sizeof($valores))-1]."<br>"; // really pick the values here. start in 1 because we'll remove 1
}
Will it always be steps of 5? Then you could do something like
$random = mt_rand(1,6) * 5;
Otherwise I'd put the numbers that you want to choose from in an array and select a random element using mt_rand():
$random_temp = array(3,6,8,13,16,19);
$random = $random_temp[mt_rand(0,6)];
If the random numbers arrayed, then this may help:
$list=array(5,10,15,20,25,30);
$n=count($list)-1;
$random=$list(rand(0,$n));
$values = [5,10,15,20,25,30];
//shuffle the array
shuffle($values);
foreach($values as $v){
print $v;
}
always in different order...
Please try this code
$random = rand(0,30);
if(($random%5)==0)
{
echo $random;
}
else
{
echo roundUpToAny($random);
}
function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}