随机数平均特定数量的 [英] Random number that averages a particular number
本文介绍了随机数平均特定数量的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来很简单,但我想一个公式(.NET preferably)其中:
对于给定的号 - 说,1.5 - 该公式将输出,接管了一系列的随机数将以年均约1.5 ...所以它可能是0.1,1.2,7.1,2.5,0.2,等等,但平均值将接近1.5。
澄清:我想这些数字是正
。解决方案
公共类RandomAroundAverage
{
随机R =新的随机();
公共双随机(双中间,双刻度)
{
返回r.NextDouble()*规模 - (刻度/ 2)+中;
}
}
然后
变种V = r.Random(1.5,20);
和它产生的随机数-8.5 - > 11.5
和看到它在行动...
变种R =新RandomAroundAverage();
VAR总和= 0.0;
的for(int i = 0; I< 10000;我++)
{
变种ν= r.Random(1.5,20);
总和+ = V;
Console.WriteLine(的String.Format(值:{0}平均:{1},V,总和/ I));
}
Seems simple, but I'd like a formula (.net preferably) which:
For a given number- say, 1.5 - the formula will output a random number which taken over a series will average around 1.5... so it could be 0.1, 1.2, 7.1, 2.5, .2, etc, but the average value will be close to 1.5.
clarification: I would like the numbers to be positive.
解决方案
public class RandomAroundAverage
{
Random r = new Random();
public double Random(double middle, double scale)
{
return r.NextDouble() * scale - (scale / 2) + middle;
}
}
then
var v = r.Random(1.5, 20);
and it will generate random numbers -8.5 -> 11.5
and to see it in action...
var r = new RandomAroundAverage();
var sum = 0.0;
for (int i = 0; i < 10000; i++)
{
var v = r.Random(1.5, 20);
sum += v;
Console.WriteLine(string.Format("Value: {0} Average: {1}", v, sum/i));
}
这篇关于随机数平均特定数量的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文