using System;
using System.Collections;
namespace RandomDemo
{
class Program
{
static void Main(string[] args)
{
Hashtable ht0 = new Hashtable();
for (int i = 0; i < 6; i++)
{
do
{
Random rnd = new Random((unchecked((int)(DateTime.Now.Ticks))));
int tmp = rnd.Next(0, 100);
if (!ht0.ContainsValue(tmp))
{
ht0.Add(i, tmp);
break;
}
}
while (true);
}
Console.WriteLine("===============");
foreach (var item in ht0.Values)
{
Console.WriteLine(item);
}
Console.WriteLine("===============");
Hashtable ht1 = new Hashtable();
for (int i = 0; i < 6; i++)
{
do
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int tmp = rnd.Next(0, 100);
if (!ht1.ContainsValue(tmp))
{
ht1.Add(i, tmp);
break;
}
}
while (true);
}
Console.WriteLine("===============");
foreach (var item in ht1.Values)
{
Console.WriteLine(item);
}
Console.WriteLine("===============");
Console.ReadKey();
}
}
}
结果1:
===============
65
90
15
41
66
91
===============
===============
91
97
92
30
90
12
===============
结果2:
===============
25
72
19
66
13
60
===============
===============
70
26
48
16
77
86
===============
结果3:
===============
76
51
26
75
50
24
===============
===============
39
33
28
83
53
50
===============
结果4:
===============
13
88
63
38
12
87
===============
===============
87
92
73
6
88
11
===============
结果5:
===============
29
4
78
53
28
2
===============
===============
64
65
46
35
94
53
===============
结果6:
===============
32
57
83
8
33
59
===============
===============
86
54
38
84
37
58
===============
So! How to get random numbers?