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.
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.


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

Open in new window

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?
CoffinatedAuthor Commented:
returned value can be any number from {1.0, 1.5, 2.0, 2.5, 3.0}, for example 2.0


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.
Get expert help—faster!

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

Ah I understand now. Try this:

function get_random($min,$max,$step)
  $numSteps = ($max-$min)/$step;
  $multiplier = rand(0,$numSteps);
  $num = $min + ($multiplier * $step);
  return $num;

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
>>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.

CoffinatedAuthor Commented:
Thank you, both solutions work, however gr8gonzo's is more efficient.
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

From novice to tech pro — start learning today.