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 Java

1 Answer

0 votes
import java.util.Random;

public class MyClass {
    public static void main(String args[]) {
        float min = 3.0f, max = 6.0f;
        Random r = new Random();
        
        for (int i = 0; i < 20; i++) {
            float f = min + r.nextFloat() * (max - min);
            System.out.println(f);
        }
    }
}




/*
run:

3.2611566
4.380795
5.139938
4.3572774
4.376444
3.8614907
3.8420928
3.6473036
3.9634087
4.6695414
4.4502244
5.7983103
5.9180026
5.236308
3.8317418
5.469215
4.0684223
4.6295447
5.7433567
3.123993

*/

 



answered Jan 28, 2023 by avibootz

Related questions

1 answer 186 views
1 answer 160 views
1 answer 124 views
1 answer 145 views
1 answer 126 views
1 answer 111 views
...