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,788 questions

51,694 answers

573 users

How to generate random floating point numbers in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomNumbers {

    // Function to generate N random numbers in the range [lower, upper]
    public static List<Double> generateRandomNumbers(int n, double lower, double upper) {
        Random rand = new Random();
        List<Double> randomNumbers = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            double num = lower + (upper - lower) * rand.nextDouble();
            randomNumbers.add(num);
        }

        return randomNumbers;
    }

    public static void main(String[] args) {
        int n = 10;
        double lower = 0.0;
        double upper = 3.0;

        List<Double> randomNumbers = generateRandomNumbers(n, lower, upper);

        for (double num : randomNumbers) {
            System.out.printf("%.6f ", num);
        }
    }
}

 
 
 
/*
run:
 
2.129351 2.384467 1.060444 1.826202 1.203413 1.944620 1.570366 2.284332 0.592021 2.781837 
 
*/

 



answered Nov 2, 2025 by avibootz
...