我是C#的初学者我创建了这个程序来生成7个随机数,并在控制台中显示它们。但现在我想生成小数位数在18.0-23.5范围内的数字。
但我无法弄清楚如何?我试过Next.Double
,但我无法让它发挥作用。请帮忙!
这是我的计划:
int lotto1 = 0, lotto2 = 0, lotto3 = 0, lotto4 = 0, lotto5 = 0, lotto6 = 0, lotto7 = 0;
Random rnd = new Random();
Console.WriteLine("Din lottorad är:");
lotto1 = rnd.Next(1, 36);
Console.WriteLine("Nr1: " + lotto1);
lotto2 = rnd.Next(1, 36);
Console.WriteLine("Nr2: " + lotto2);
lotto3 = rnd.Next(1, 36);
Console.WriteLine("Nr3: " + lotto3);
lotto4 = rnd.Next(1, 36);
Console.WriteLine("Nr4: " + lotto4);
lotto5 = rnd.Next(1, 36);
Console.WriteLine("Nr5: " + lotto5);
lotto6 = rnd.Next(1, 36);
Console.WriteLine("Nr6: " + lotto6);
lotto7 = rnd.Next(1, 36);
Console.WriteLine("Nr7: " + lotto7);
答案 0 :(得分:8)
使用NextDouble
:
rnd.NextDouble() * (23.5-18.0) + 18.0
或更一般地说:
public double NextDouble(Random rnd, double min, double max)
{
return rnd.NextDouble() * (max-min) + min;
}