Jump to content

Random Float Generation


Kaizard

Recommended Posts

What is the best solution for generating random float numbers and having min-max? mt_rand doesn't help much by itself and needs to have some conversion. I found this line on a site:

 

min + rang * mt_rand()/RAND_MAX - Converts random integers to random real numbers.

 

but I can't figure out what rang is and where i should insert the min max values. The generator on the site is perfect for what i need, I just need help on the code behind.

 

Here's the site link: http://sqa.fyicenter.com/Online_Test_Tools/Random_Real_Number_Float_Value_Generator.php

 

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/184062-random-float-generation/
Share on other sites

If you want to set how many decimals you want and then just go from there, you can easily just calculate a random integer and shift the decimal over. Eg. A pseudo-random floating point number between 1-1000 with 3 decimals could just be mt_rand(1*10^3, 1000*10^3)/10^3 (note: not integer division).

 

In their method rang is probably the range of points (max - min). min is the lowest number, and RAND_MAX I would assume is a constant whose value is the same as mt_getrandmax.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.