我正在關注隨機數生成在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();
}
}
}
曾幾何時我花了幾個小時的調試一些代碼,才發現出(if(condition)statement1; statement2;'而不是'if(condition){statement1; statement2;}'現在每次我看到一些代碼在純懶惰中省略括號,(if(條件)聲明'而不是'如果(條件){語句}')它發送我一個殺人的憤怒 – NullUserException