亂碼產生器

来源:互联网 发布:大数据行业股票 编辑:程序博客网 时间:2024/06/06 07:37

using System;
using System.Security.Cryptography;

/// <summary>
/// 亂碼產生器
/// </summary>
public static class RandomCreator
{
    private static RNGCryptoServiceProvider rngp = new RNGCryptoServiceProvider();

    private static byte[] rb = new byte[4];

    /// <summary>
    /// 產生亂數
    /// </summary>
    /// <returns>亂數</returns>
    private static Int32 CreateNumber()
    {
        rngp.GetBytes(rb);

        return BitConverter.ToInt32(rb, 0);
    }
    
    /// <summary>
    /// 產生一個非負數且最大值以下的亂數
    /// </summary>
    /// <param name="maxValue">最大值</param>
    ///
    public static int Next(int maxValue)
    {
        int value = CreateNumber();

        value = value % (maxValue + 1);

        if (value < 0)
        {
            return -value;
        }

        return value;
    }

    
}

0 0