Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to generate random float in specific range with PHP

1 Answer

0 votes
function rand_float($min, $max, $decimals = 0) {
    $dec = pow(10, $decimals);
    return mt_rand($min * $dec, $max * $dec) / $dec;
}

for ($i = 0; $i < 20; $i++) {
    echo rand_float(2, 7, 3) . "\n";
}




/*
run:

5.246
3.79
4.208
2.39
3.947
5.749
3.607
5.481
2.751
6.632
5.682
3.058
5.408
6.14
6.39
3.877
3.674
2.806
3.714
3.045

*/

 



answered Jun 2, 2022 by avibootz

Related questions

1 answer 110 views
1 answer 160 views
1 answer 124 views
1 answer 125 views
1 answer 110 views
2 answers 115 views
...