特大号之间的关系随机数 [英] Random Number Between 2 Double Numbers

查看:141
本文介绍了特大号之间的关系随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的2双打之间产生一个随机数?

It is possible to generate a random number between 2 doubles?

例如:

public double GetRandomeNumber(double minimum, double maximum)
{
    return Random.NextDouble(minimum, maximum) 
}

然后我把它改为:

Then I call it with the following:

double result = GetRandomNumber(1.23, 5.34);

任何想法,将AP preciated。

Any thoughts would be appreciated.

谢谢!

推荐答案

Random.NextDouble返回0和1之间的双您再乘上该范围内,你需要进入(最大值与最小值之差),然后添加到基础(最小)。

Random.NextDouble returns a double between 0 and 1. You then multiply that by the range you need to go into (difference between maximum and minimum) and then add that to the base (minimum).

public double GetRandomNumber(double minimum, double maximum)
{ 
    Random random = new Random();
    return random.NextDouble() * (maximum - minimum) + minimum;
}

皇家code应该有随意的静态成员。这将节省创建随机数生成器的成本,将使您致电getRandomNumber的非常频繁。由于我们初始化一个新的RNG与每一个电话,如果调用足够快的系统时间不通话RNG会得到完全相同的时间戳之间播种改变,并产生随机数相同的流。

Real code should have random be a static member. This will save the cost of creating the random number generator, and will enable you to call GetRandomNumber very frequently. Since we are initializing a new RNG with every call, if you call quick enough that the system time doesn't change between calls the RNG will get seeded with the exact same timestamp, and generate the same stream of random numbers.

这篇关于特大号之间的关系随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆