关键代码:
/// <summary> /// 生成设置范围内的Double的随机数 /// eg:_random.NextDouble(1.5, 2.5) /// </summary> /// <param name="random">Random</param> /// <param name="miniDouble">生成随机数的最大值</param> /// <param name="maxiDouble">生成随机数的最小值</param> /// <returns>当Random等于NULL的时候返回0;</returns> public static double NextDouble(this Random random, double miniDouble, double maxiDouble) { if (random != null) { return random.NextDouble() * (maxiDouble - miniDouble) + miniDouble; } else { return 0.0d; } }
测试代码:
static void Main(string[] args) { try { Random _random = new Random(); for (int i = 0; i < 10; i++) { Console.WriteLine(_random.NextDouble(1.5, 2.5)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadLine(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }测试结果:
[C#]NextDouble
时间: 2024-10-09 02:07:21