dotnetandsqldevelop
dotnetandsqldevelop

Reputation: 365

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.

Upvotes: 19

Views: 47130

Answers (7)

BHGB
BHGB

Reputation: 11

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();

Upvotes: 1

elie michael ngandu
elie michael ngandu

Reputation: 11

here my solution, it's not pretty but it works well

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

Upvotes: 1

dognose
dognose

Reputation: 20899

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;

Upvotes: 11

Ben Stabile
Ben Stabile

Reputation: 173

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 (e.g. 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!

Upvotes: 1

Guray Tonguc
Guray Tonguc

Reputation: 11

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;

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

Random r = new Random();
var number = r.Next(141421, 314160) / 100000M;

Also you can't force decimal number to match your pattern. E.g. 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");

Upvotes: 6

Erik Schierboom
Erik Schierboom

Reputation: 16636

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)

Upvotes: 34

Related Questions