zoukankan      html  css  js  c++  java
  • 【C#】绝对随机数

    System.Security.Cryptography.RNGCryptoServiceProvider

            private static int Next(int numSeeds, int length)
            {
                // Create a byte array to hold the random value.   
                byte[] buffer = new byte[length];
                // Create a new instance of the RNGCryptoServiceProvider.   
                System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
                // Fill the array with a random value.   
                Gen.GetBytes(buffer);
                // Convert the byte to an uint value to make the modulus operation easier.   
                uint randomResult = 0x0;//这里用uint作为生成的随机数   
                for (int i = 0; i < length; i++)
                {
                    randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
                }
                // Return the random number mod the number   
                // of sides.  The possible values are zero-based   
                return (int)(randomResult % numSeeds);
            }

    --

                for (int i = 0; i < 20; i++)
                {
                    byte[] randomBytes = new byte[8];
                    System.Security.Cryptography.RNGCryptoServiceProvider rngServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider();
                    rngServiceProvider.GetBytes(randomBytes);
                    int result = BitConverter.ToInt32(randomBytes, 0);
                    result = System.Math.Abs(result);  //求绝对值
                    Console.WriteLine(result);
                }
  • 相关阅读:
    c++ 函数
    c++ 数字
    c++语句(判断)
    十九、夜间模式的开启与关闭,父模板的制作
    二十、开始Flask项目
    十八、完成登录与注册页面的前端
    十七、JavaScript 基础,登录前端验证
    CSS实例:图片导航块
    十五、导航,头部,CSS基础
    十四、web基础,用html元素制作web页面
  • 原文地址:https://www.cnblogs.com/oiliu/p/7753096.html
Copyright © 2011-2022 走看看