已知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;
相关新闻>>
- 发表评论
-
- 最新评论 更多>>