17

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.

4

7 回答 7

34

您可以轻松定义一个返回两个值之间的随机数的方法:

private static readonly Random random = new Random();

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

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

然后,您可以使用所需的值调用此方法:

RandomNumberBetween(1.41421, 3.14159)
于 2013-07-22T11:55:31.093 回答
10

使用这样的东西。

Random random = new Random()
int r = random.Next(141421, 314160); //+1 as end is excluded.
Double result = (Double)r / 100000.00;
于 2013-07-22T11:49:38.640 回答
6
Random r = new Random();
var number = r.Next(141421, 314160) / 100000M;

此外,您不能强制十进制数与您的模式匹配。例如,如果您有1.5数字,它将与1.50000格式不匹配。因此,您应该将结果格式化为字符串:

string formattedNumber = number.ToString("0.00000");
于 2013-07-22T11:50:10.017 回答
1

我用过这个。我希望这有帮助。

Random Rnd = new Random();

double RndNum = (double)(Rnd.Next(Convert.ToInt32(LatRandMin.Value), Convert.ToInt32(LatRandMax.Value)))/1000000;
于 2013-08-25T21:58:06.143 回答
1

查看以下链接以获取应有帮助的现成实现:

MathNet.Numerics、随机数和概率分布

广泛的分布特别令人感兴趣,它建立在直接从 System.Random 派生的随机数生成器(MersenneTwister 等)之上,所有这些都提供了方便的扩展方法(例如 NextFullRangeInt32、NextFullRangeInt64、NextDecimal 等)。当然,您可以只使用默认的 SystemRandomSource,它只是使用扩展方法修饰的 System.Random。

哦,如果需要,您可以将 RNG 实例创建为线程安全的。

确实很方便!

于 2014-02-05T01:29:27.720 回答
1

这是我的解决方案,它不漂亮,但效果很好

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

于 2017-06-02T16:20:58.873 回答
1
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();
于 2017-10-10T02:57:06.983 回答