You need to get a random number in the range from 00.00 to 100.00

Is there a solution for this question?

  • Create artificially tenths and hundredths of a larger number, for example, by converting to float and using round - Dmitriy Gvozd
  • 3
    echo round(rand(0, 10000)/100,2); - Ipatiev
  • @ Ipatiev, do you think if you divide an integer by 100, there will be more than 2 digits after the decimal point? - Visman
  • @Visman, thinks that more is not required to answer the question - ArchDemon
  • one
    @ Alexey Shimansky, if the emulator is not lying, then nothing extra will appear: sandbox.onlinephpfunctions.com/code/… - Visman

3 answers 3

Thanks for the decision @ Ipatiev. Slightly modified code:

 return number_format(rand(0, 10000) / 100, 2, '.', ''); 
  • Please do not reply with the phrase "thank you". Instead, mark the best answer as accepted (the daw opposite the selected answer). - From the queue of checks - Streletz
  • @Streletz the correct answer was given in the comments. - ikerya
  • Then it makes sense and thank in the comments. Simply, according to the rules of the site, the field "Your answer" is precisely for answering questions. Nothing personal. - Streletz

Try this solution.

 function random_float ($min,$max) { return ($min+lcg_value()*(abs($max-$min))); } 
  • An example of the number returned by this function -> 23.388192741654 - Visman
  • AND? round($var, 2); Complicated? - Dmitry Gvozd
  • Yes, very difficult! - Visman

Code

 echo rand(0, 10000)/100; 

generates numbers from 0 to 100, with a maximum of 2 decimal places.

Code with rounding

 echo round(rand(0, 10000)/100,2); 

redundant. Test here http://sandbox.onlinephpfunctions.com/code/c5cebbfd6357874724f07708ad22e8c52055087f