给定数字之间的随机双 [英] Random double between given numbers
本文介绍了给定数字之间的随机双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在寻找一些简洁,现代的C#代码来生成之间的随机双数 1.41421
和 3.14159
。其中数字应为 [0-9] {1} [0-9] {5}
格式。
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.
我在想一些解决方案,利用 Enumerable.Range
在某种程度上,可以使这种更加简洁。
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)
这篇关于给定数字之间的随机双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文