我正在关注Visual C#2010 Express中随机数生成的教程作为控制台应用程序并遇到了问题。
代码与我正在观看的教程相匹配,据我所知,有5个概述。
这个想法是它将采用内置的随机种子函数并用它生成100个值。程序将存储随机数(双精度)作为DICTIONARY的KEY和int VALUE以显示重复数。该系统将打印有关重复,平均值和分布的分析数据。
//PROGRAM SCOPE
private const int Count = 100;
// MAIN CLASS SCOPE
var standardRnd = new Random(20);
var list = new Dictionary<double,int>();
//In a for loop till Count-1
var rand = standardRnd.NextDouble();
if (!list.ContainsKey(rand))
list.Add(rand, 1);
else
{
list[rand]++;
duplicate++;
}
当我运行该程序时,控制台将激活,但我不会在屏幕上打印任何内容。大约一秒钟之后它将抛出异常并突出显示此代码段。
sum += rand * list[rand];
错误:字典中没有给定的密钥。
从逻辑上讲,这对我来说很有意义。你们看到有什么不对劲的吗?
感谢您提供任何帮助。
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Random_RnD
{
class Program
{
static void Main(string[] args)
{
var standardRnd = new Random(20);
var min = 1.0;
var max = 0.0;
var valueCounter = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
var list = new Dictionary<double,int>();
var duplicate = 0;
var iterations = 0;
var timeStart = DateTime.Now;
//Do Stuff
for (int x = 0; x < Count; x++)
{
var rand = standardRnd.NextDouble();
if (!list.ContainsKey(rand))
list.Add(rand, 1);
else
{
list[rand]++;
duplicate++;
}
iterations++;
}
var timeStop = DateTime.Now;
var elapseTime = TimeSpan.FromTicks((timeStop-timeStart).Ticks);
//Analytics
var sum = 0.0;
foreach (var kvp in list)
{
var rand = kvp.Key;
if (rand < min)
min = rand;
if(rand>max)
rand = max;
if (rand >= 0.0 && rand < 0.1)
valueCounter[0]++;
if (rand >= 0.1 && rand < 0.2)
valueCounter[1]++;
if (rand >= 0.2 && rand < 0.3)
valueCounter[2]++;
if (rand >= 0.3 && rand < 0.4)
valueCounter[3]++;
if (rand >= 0.4 && rand < 0.5)
valueCounter[4]++;
if (rand >= 0.5 && rand < 0.6)
valueCounter[5]++;
if (rand >= 0.6 && rand < 0.7)
valueCounter[6]++;
if (rand >= 0.7 && rand < 0.8)
valueCounter[7]++;
if (rand >= 0.8 && rand < 0.9)
valueCounter[8]++;
if (rand >= 0.9 && rand <= 1.0)
valueCounter[9]++;
sum += rand * list[rand];
}
Console.WriteLine("{0:###,###,###} Iteration took: {1:D2}min:{2:D2}sec:{3:D2}msec",
iterations, elapseTime.Minutes, elapseTime.Seconds, elapseTime.Milliseconds);
Console.WriteLine(" {0}\n {1}", min, max);
Console.WriteLine(" 0.0 to 0.1 = {0:###,###,###}", valueCounter[0]);
Console.WriteLine(" 0.1 to 0.2 = {0:###,###,###}", valueCounter[1]);
Console.WriteLine(" 0.2 to 0.3 = {0:###,###,###}", valueCounter[2]);
Console.WriteLine(" 0.3 to 0.4 = {0:###,###,###}", valueCounter[3]);
Console.WriteLine(" 0.4 to 0.5 = {0:###,###,###}", valueCounter[4]);
Console.WriteLine(" 0.5 to 0.6 = {0:###,###,###}", valueCounter[5]);
Console.WriteLine(" 0.6 to 0.7 = {0:###,###,###}", valueCounter[6]);
Console.WriteLine(" 0.7 to 0.8 = {0:###,###,###}", valueCounter[7]);
Console.WriteLine(" 0.8 to 0.9 = {0:###,###,###}", valueCounter[8]);
Console.WriteLine(" 0.9 to 1.0 = {0:###,###,###}", valueCounter[9]);
var avg = sum / (double)iterations;
Console.WriteLine("\nStatistics:");
Console.WriteLine(" Mean = {0}", avg);
Console.WriteLine(" Duplicates = {0}", duplicate);
Console.ReadKey();
}
}
}
答案 0 :(得分:2)
问题在于:
if (rand>max)
rand = max;
这为rand分配0。但是0并不保证在字典中。
您的意思是max = rand;
。
使用LINQ:
还可以更轻松地查找集合中的最小值和最大值var keys = list.Keys;
double min = keys.Min();
double max = keys.Max();