Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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