C#如何产生散列性比较好的随机数序列?时间用多一些都可以~~

解决方案 »

  1.   

    使用加密服务提供程序 (CSP) 提供的实现来实现加密随机数生成器 (RNG):RNGCryptoServiceProvider类,
    命名空间:System.Security.CryptographyMSDN的例子:
    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;class RNGCSP
    {
    // Main method.
        public static void Main()
        {
            // Roll the dice 30 times and display 
            // the results to the console.
            for(int x = 0; x <= 30; x++)
                Console.WriteLine(RollDice(6));
        }
        
    // This method simulates a roll of the dice. The input parameter is the 
    // number of sides of the dice.
        public static int RollDice(int NumSides)
        {
            // Create a byte array to hold the random value.
            byte[] randomNumber = new byte[1];        // Create a new instance of the RNGCryptoServiceProvider. 
            RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();        // Fill the array with a random value.
            Gen.GetBytes(randomNumber);        // Convert the byte to an integer value to make the modulus operation easier.
            int rand = Convert.ToInt32(randomNumber[0]);        // Return the random number mod the number
            // of sides.  The possible values are zero-
            // based, so we add one.
            return rand % NumSides + 1;
        }
    }
      

  2.   

    比喻说:RollDice(100) 将生成1到100
    其中这种规律对于1到256都有效,但是数字RollDice(257)不能生成最大值257!
      

  3.   

    多生成两次接到一起不就行了?
    RollDice(250) * 250 + RollDice(250)