Code
static void Main(string[] args)
{
List<Int32> list = new List<Int32>();
Random rand = new Random();
do
{
int number = rand.Next(1000000, 9999999);
if (!list.Contains(number) && !isPrimeNumber(number))
{
list.Add(number);
}
}
while (list.Count < 100);
list.Sort();
foreach (int item in list)
{
Console.WriteLine(item);
}
}
private static bool isPrimeNumber(int number)
{
//返回 number是否是素数
}
传说Guid是不会重复的,并且Guid的HashCode也很少重复,所以先生成100个Guid的HashCode,然后再挑重复的,此时重复的可能性已经非常非常小了,挑去重复的再用新的Guid补上,效率可能会好些
Code
using System.Collections;
class PrimeNumber
{
Dictionary<int, int> _PrimeNumberDict = new Dictionary<int,int>();
public PrimeNumber(int maxNumber)
{
BitArray numbers = new BitArray(maxNumber, true);
for (int i = 2; i < maxNumber; i++)
{
if (numbers[i])
{
for (int j = i * 2; j < maxNumber; j += i)
{
numbers[j] = false;
}
}
}
for (int i = 1; i < maxNumber; i++)
{
if (numbers[i])
{
_PrimeNumberDict.Add(i, i);
}
}
}
public bool IsPrimeNumber(int n)
{
return _PrimeNumberDict.ContainsKey(n);
}
}
static void Main(string[] args)
{
PrimeNumber pn = new PrimeNumber(9999999);
SortedDictionary<int, int> list = new SortedDictionary<int, int>();
Random rand = new Random();
do
{
int number = rand.Next(1000000, 9999999);
if (!pn.IsPrimeNumber(number))
{
if (!list.ContainsKey(number))
{
list.Add(number, number);
}
}
}
while (list.Count < 100);
foreach (int item in list.Keys)
{
Console.WriteLine(item);
}
}