generate random between min/max with step value

I need to create a function that takes 3 parameters "min", "max", "step" the purpose of it is to generate a random number between min-max, but in increments of "step"
For example get_random(1,3,0.5) would return a single value of {1.0, 1.5, 2.0, 2.5, 3.0}

The "step" value can be pretty much anything (1, 2, 0.001).

Appreciate any help.
LVL 5
CoffinatedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try:


function get_random($mi,$ma,$st){
 $result=array();
 for( $i=$mi; $i<$ma; $i+=$st)
 {
  $result[]=$i;
 }
return $result;
}

Open in new window

0
gr8gonzoConsultantCommented:
I'm not sure where it's random - are you looking to generate a random STARTING number between those values and increment from there? For example, if you ran your get_random(1,3,0.5) four times, what would the output look like on each of those 4 runs?
0
CoffinatedAuthor Commented:
clarification:
returned value can be any number from {1.0, 1.5, 2.0, 2.5, 3.0}, for example 2.0

hielo:

That will almost work, returned value needs to be a random number from the array.
$total = count($result);
return $result[rand(0, $total)];

That's was the function I had in mind, I was hoping for something more sophisticated in PHP, but it'll do just fine.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

gr8gonzoConsultantCommented:
Ah I understand now. Try this:

<?php
function get_random($min,$max,$step)
{
  $numSteps = ($max-$min)/$step;
  $multiplier = rand(0,$numSteps);
  $num = $min + ($multiplier * $step);
  return $num;
}
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
>>returned value needs to be a random number from the array.
Ah, OK. That was not clear from what you posted :)

You obviously figured out the extra code to get that random number.

Regards,
Hielo
0
CoffinatedAuthor Commented:
Thank you, both solutions work, however gr8gonzo's is more efficient.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.