简体   繁体   中英

Random double between given numbers

I'm looking for some succinct, modern C# code to generate a random double number between 1.41421 and 3.14159 . where the number should be [0-9]{1}.[0-9]{5} format.

I'm thinking some solution that utilizes Enumerable.Range somehow may make this more succinct.

You can easily define a method that returns a random number between two values:

private static readonly Random random = new Random();

private static double RandomNumberBetween(double minValue, double maxValue)
{
    var next = random.NextDouble();

    return minValue + (next * (maxValue - minValue));
}

You can then call this method with your desired values:

RandomNumberBetween(1.41421, 3.14159)

Use something like this.

Random random = new Random()
int r = random.Next(141421, 314160); //+1 as end is excluded.
Double result = (Double)r / 100000.00;
Random r = new Random();
var number = r.Next(141421, 314160) / 100000M;

Also you can't force decimal number to match your pattern. Eg if you have 1.5 number it will not match 1.50000 format. So, you should format result as string:

string formattedNumber = number.ToString("0.00000");

I used this. I hope this helps.

Random Rnd = new Random();

double RndNum = (double)(Rnd.Next(Convert.ToInt32(LatRandMin.Value), Convert.ToInt32(LatRandMax.Value)))/1000000;

Check out the following link for ready-made implementations that should help:

MathNet.Numerics, Random Numbers and Probability Distributions

The extensive distributions are especially of interest, built on top of the Random Number Generators (MersenneTwister, etc.) directly derived from System.Random, all providing handy extension methods (eg NextFullRangeInt32, NextFullRangeInt64, NextDecimal, etc.). You can, of course, just use the default SystemRandomSource, which is simply System.Random embellished with the extension methods.

Oh, and you can create your RNG instances as thread safe if you need it.

Very handy indeed!

在这里,我的解决方案,它不漂亮,但它运作良好

Random rnd = new Random(); double num = Convert.ToDouble(rnd.Next(1, 15) + "." + rnd.Next(1, 100)); Console.WriteLine(num); Console.ReadKey();

JMH BJHBJHHJJ const int SIZE = 1;
        int nnOfTosses,

            headCount = 0, tailCount = 0;
        Random tossResult = new Random();

        do
        {
            Console.Write("Enter integer number (>=2) coin tosses or 0 to Exit: ");
            if (!int.TryParse(Console.ReadLine(), out nnOfTosses))
            {
                Console.Write("Invalid input");
            }
            else
            {

                //***//////////////////
                // To assign a random number to each element
                const int ROWS = 3;
                double[] scores = new double[ROWS];
                Random rn = new Random();

                // Populate 2D array with random values
                for (int row = 0; row < ROWS; row++)
                {
                    scores[row] = rn.NextDouble();
                }
                //***//////////////////////////

                for (int i = 0; i < nnOfTosses; i++)
                {

                    double[] tossResult = new double[i];
                    tossResult[i]= tossResult.nextDouble();

                }
                Console.Write("Number of Coin Tosses = " + nnOfTosses);
                Console.Write("Fraction of Heads = ");
                Console.Write("Fraction of Tails = ");
                Console.Write("Longest run is ");
            }
        } while (nnOfTosses != 0);

        Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM