我正在关注 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();
}
}
}