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 C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    // Function to generate N random numbers in the range [lower, upper]
    static List<double> GenerateRandomNumbers(int count, double lower, double upper) {
        var rand = new Random();
        var numbers = new List<double>();

        for (int i = 0; i < count; i++) {
            double num = lower + (upper - lower) * rand.NextDouble();
            numbers.Add(num);
        }

        return numbers;
    }

    static void Main()
    {
        int count = 10;
        double lower = 0.0, upper = 3.0;

        List<double> randomNumbers = GenerateRandomNumbers(count, lower, upper);

        foreach (double num in randomNumbers) {
            Console.Write($"{num:F6} ");
        }

        Console.WriteLine();
    }
}


 
 
/*
run:
 
2.622065 1.014979 2.241009 2.769607 2.898364 0.980846 2.211666 1.046340 2.925195 0.020147 
 
*/

 



answered Nov 2, 2025 by avibootz
...