C# 给定数字之间的随机双倍
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17786771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random double between given numbers
提问by dotnetandsqldevelop
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.
我正在寻找一些简洁的现代 C# 代码来生成1.41421
和之间的随机双数3.14159
。数字应该是[0-9]{1}.[0-9]{5}
格式的地方。
I'm thinking some solution that utilizes Enumerable.Range
somehow may make this more succinct.
我在想一些Enumerable.Range
以某种方式利用的解决方案可能会使这更简洁。
采纳答案by Erik Schierboom
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)
回答by dognose
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;
回答by Sergey Berezovskiy
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:
你也不能强制十进制数来匹配你的模式。例如,如果您有1.5
数字,它将与1.50000
格式不匹配。因此,您应该将结果格式化为字符串:
string formattedNumber = number.ToString("0.00000");
回答by Guray Tonguc
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;
回答by Ben Stabile
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.
广泛的分布尤其令人感兴趣,它们建立在直接从 System.Random 派生的随机数生成器(MersenneTwister 等)之上,都提供了方便的扩展方法(例如 NextFullRangeInt32、NextFullRangeInt64、NextDecimal 等)。当然,您可以只使用默认的 SystemRandomSource,它只是用扩展方法修饰的 System.Random。
Oh, and you can create your RNG instances as thread safe if you need it.
哦,如果需要,您可以将 RNG 实例创建为线程安全的。
Very handy indeed!
确实很方便!
回答by elie michael ngandu
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();
Random rnd = new Random();
double num = Convert.ToDouble(rnd.Next(1, 15) + "." + rnd.Next(1, 100));
Console.WriteLine(num);
Console.ReadKey();
回答by BHGB
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();