Random Doubling Between Preset Numbers

I am looking for a short, 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} .

I think some kind of solution using Enumerable.Range can make this more concise.

+8
c #
source share
7 answers

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

Then you can call this method with your desired values:

 RandomNumberBetween(1.41421, 3.14159) 
+17
source share

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; 
+9
source share
 Random r = new Random(); var number = r.Next(141421, 314160) / 100000M; 

Also, you cannot force a decimal number to match your pattern. For example. if you have number 1.5 , it will not correspond to the format of 1.50000 . So you should format the result as a string:

 string formattedNumber = number.ToString("0.00000"); 
+5
source share

I used this. Hope this helps.

 Random Rnd = new Random(); double RndNum = (double)(Rnd.Next(Convert.ToInt32(LatRandMin.Value), Convert.ToInt32(LatRandMax.Value)))/1000000; 
0
source share

here is my solution, it is not very, 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();

0
source share
 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(); 
0
source share

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

MathNet.Numerics, Random Numbers and Probability Distributions

Extensive distributions based on random number generators (MersenneTwister, etc.) directly obtained from System.Random are of particular interest, all of which provide convenient extension methods (for example, NextFullRangeInt32, NextFullRangeInt64, NextDecimal, etc.). You can, of course, simply use the default SystemRandomSource, which is simply System.Random, decorated with extension methods.

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

Very comfortably!

-one
source share

All Articles