已知Random.Next(1,10),产生1至100不重复的随机数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace www.cnblogs.com.rock_chen
{
class Program
{
/// <summary>
/// 已知Random.Next(1,10),产生1至100不重复的随机数据
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
GenerateRandom generateRandom = new GenerateRandom();
var result = generateRandom.Calc();
Console.WriteLine(string.Join(",", result));
Console.WriteLine();
Console.WriteLine(string.Join(",", result.OrderBy(p => p).ToArray()));
Console.Read();
}
}
public class GenerateRandom
{
Random random = new Random();
public int[] Calc()
{
int[] ary = new int[100];
int count = 0;
int currentIndex = 0;
int k = 0;
while (count < 100)
{
var index = RandomInt();
if (ary[currentIndex] == 0)
{
ary[currentIndex] = index;
ary[index - 1] = currentIndex + 1;
currentIndex = index - 1;
count = 2;
continue;
}
if (ary[index - 1] == 0 && currentIndex != index - 1)
{
ary[index - 1] = ary[currentIndex];
ary[currentIndex] = index;
currentIndex = index - 1;
count++;
}
else
{
while (k < 100 && ary[k] > 0)
{
k++;
}
if (currentIndex != k && k < 100)
{
ary[k] = ary[currentIndex];
ary[currentIndex] = k + 1;
currentIndex = k;
count++;
}
}
}
return ary;
}
public int RandomInt()
{
return (random.Next(1, 10) - 1) * 10//10-80十位数
+ random.Next(1, 10) + random.Next(1, 10) - 1 //1_17
+ (random.Next(1, 10) & 3); //0-3
}
}
}
摘自 I'm CY
补充:Web开发 , ASP.Net ,