Before Fix (Horrible) :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp_RandNumber_1029
{
public partial class Form1 : Form
{
int numberMax = 0, numberMin = 0,
numberSize = 0, numberCount = 0,
numberStart = 0, numberEnd = 0,
numberSearch = 0, numberRand = 0,
numReg = 0 , saveCount = 0,
totalTimes = 0;
int[] numberArray, valueArray, countArray;
Random randSeed = new Random();
private void btn_Click_Click(object sender, EventArgs e)
{
if (checkInputValueType())
{
checkValueSequence(numberStart, numberEnd);
numReg = 0;
saveCount = 0;
totalTimes = 0;
dataView_Result.Rows.Clear();
numberSize = numberMax - numberMin + 1;
numReg = numberMin;
numberArray = new int[numberSize];
valueArray = new int[numberSize];
countArray = new int[numberSize];
for (int i = 0; i < numberSize; ++i)
{
numberArray[i] = i+1;
valueArray[i] = numReg;
numReg += 1;
countArray[i] = 0;
}
for (int j = 0; j < numberCount; ++j)
{
numberRand = randSeed.Next(numberMin, (numberMax+1));
for (int k = 0; k < valueArray.Count(); ++k)
{
if (numberRand.Equals(valueArray[k]))
{
saveCount = countArray[k];
saveCount += 1;
countArray[k] = saveCount;
}
}
}
for (int m= 0; m < numberArray.Count(); ++m)
{
totalTimes = totalTimes + countArray[m];
dataView_Result.Rows.Add(numberArray[m], valueArray[m],
countArray[m], totalTimes);
}
if (numberSearch > numberMax || numberSearch < numberMin)
{
MessageBox.Show(@"Search Target Not Exist !");
}
else
{
for (int n = 0; n < valueArray.Count(); ++n)
{
if (numberSearch.Equals(valueArray[n]))
{
MessageBox.Show(@"Target Is: " + valueArray[n] +
" , " + countArray[n] + " Times");
}
}
}
}
else
{
MessageBox.Show("Error Values !");
}
}
public Form1()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;
dataResult();
}
private bool checkInputValueType()
{
bool valueTypeOK = int.TryParse(tBox_Start.Text, out numberStart);
if (!valueTypeOK)
{
MessageBox.Show(@"Only Int Type Value !");
return false;
}
valueTypeOK = int.TryParse(tBox_End.Text, out numberEnd);
if (!valueTypeOK)
{
MessageBox.Show(@"Only Int Type Value !");
return false;
}
valueTypeOK = int.TryParse(tBox_Count.Text, out numberCount);
if (!valueTypeOK)
{
MessageBox.Show(@"Only Int Type Value !");
return false;
}
else
{
if (!(numberCount > 0))
{
MessageBox.Show(@"Only Positive Values !");
return false;
}
}
valueTypeOK = int.TryParse(tBox_Search.Text, out numberSearch);
if (!valueTypeOK)
{
MessageBox.Show(@"Only Int Type Value !");
return false;
}
return true;
}
private void checkValueSequence(int _first, int _second)
{
if (_first > _second)
{
numberMax = _first;
numberMin = _second;
}
else if (_first < _second)
{
numberMax = _second;
numberMin = _first;
}
else
numberMax = numberMin = _first;
}
private void dataResult()
{
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.HeaderText = "Sequence";
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.HeaderText = "Random Number";
DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
column3.HeaderText = "Times";
DataGridViewTextBoxColumn column4 = new DataGridViewTextBoxColumn();
column4.HeaderText = "Cumulative Times";
dataView_Result.Columns.Clear();
dataView_Result.Columns.Add(column1);
dataView_Result.Columns.Add(column2);
dataView_Result.Columns.Add(column3);
dataView_Result.Columns.Add(column4);
}
}
}
After Fix (Not Best, But More Clean) :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp_RandNumber_1029
{
public partial class Form1 : Form
{
Random randSeed = new Random();
private void ButtonClickEvent(object sender, EventArgs e)
{
int valueStart, valueEnd,
valueCount,
valueMax, valueMin,
valueSearch,
sumCount = 0;
Dictionary<int, int> numberDict = new Dictionary<int, int>();
if ( int.TryParse(tBox_Start.Text, out valueStart) &&
int.TryParse(tBox_End.Text, out valueEnd) &&
(int.TryParse(tBox_Count.Text, out valueCount) &&
valueCount > 0) &&
int.TryParse(tBox_Search.Text, out valueSearch))
{
valueMax = Math.Max(valueStart, valueEnd);
valueMin = Math.Min(valueStart, valueEnd);
for (int i=0; i<valueCount; ++i)
{
int randNum = randSeed.Next(valueMin, valueMax + 1);
if (!numberDict.ContainsKey(randNum))
{
numberDict.Add(randNum, 1);
}
else
{
numberDict[randNum] += 1;
}
}
var numQuery = from num in numberDict
orderby num.Key
select num;
dataView_Result.Rows.Clear();
foreach (KeyValuePair<int, int> kvp in numQuery)
{
sumCount += kvp.Value;
dataView_Result.Rows.Add( kvp.Key,
kvp.Value,
sumCount);
}
if (numberDict.Keys.Contains(valueSearch))
{
string howMuchTimes = numberDict[valueSearch] > 1 ?
" Times" : " Time";
MessageBox.Show(@"Find Target: " + valueSearch +
" , " + numberDict[valueSearch] + howMuchTimes);
}
else
{
MessageBox.Show(@"Can Not Find Target !");
}
}
else
{
MessageBox.Show(@"Input Value Type Error !");
}
}
public Form1()
{
InitializeComponent();
btn_Click.Click += new EventHandler(ButtonClickEvent);
InitialResult();
}
private void InitialResult()
{
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.HeaderText = "Random Number";
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.HeaderText = "Result Count";
DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
column3.HeaderText = "Sum Of Total Count";
dataView_Result.Columns.Clear();
dataView_Result.Columns.Add(column1);
dataView_Result.Columns.Add(column2);
dataView_Result.Columns.Add(column3);
}
}
}