生成不重复的随机数组

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        private static int ArrayLength = 1000;

        static void Main(string[] args)
        {
            var array = Enumerable.Range(1, ArrayLength)
                .OrderBy(n => GetRandomSeed())
                .ToArray<int>();

            Console.ReadLine();

        }

        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
    }
}

  

posted on 2016-07-11 14:59  Groot.Boom  阅读(103)  评论(0编辑  收藏  举报