zoukankan      html  css  js  c++  java
  • silverlight 2 Random 随机数解决方案

    using System;
    using System.Security.Cryptography;

        public class RNG
        {
            
    private static RNGCryptoServiceProvider rngp = new RNGCryptoServiceProvider();
            
    private static byte[] rb = new byte[4];

            
    /// <summary>
            
    /// 产生一个非负数的乱数
            
    /// </summary>
            public static int Next()
            {
                rngp.GetBytes(rb);
                
    int value = BitConverter.ToInt32(rb, 0);
                
    if (value < 0) value = -value;
                
    return value;
            }
            
    /// <summary>
            
    /// 产生一个非负数且最大值在 max 以下的乱数
            
    /// </summary>
            
    /// <param name="max">最大值</param>
            public static int Next(int max)
            {
                rngp.GetBytes(rb);
                
    int value = BitConverter.ToInt32(rb, 0);
                value 
    = value % (max + 1);
                
    if (value < 0) value = -value;
                
    return value;
            }
            
    /// <summary>
            
    /// 产生一个非负数且最小值在 min 以上最大值在 max 以下的乱数
            
    /// </summary>
            
    /// <param name="min">最小值</param>
            
    /// <param name="max">最大值</param>
            public static int Next(int min, int max)
            {
                
    int value = Next(max - min) + min;
                
    return value;
            }

        }
  • 相关阅读:
    Java学习
    Java学习
    Java学习
    Java学习
    Java学习
    Java 泛型(六):泛型
    Java 泛型(五):泛型
    第十六章:垃圾回收(Garbage Collection)相关概念
    Java 泛型(四):泛型
    Java 泛型(三):泛型在继承方面体现与通配符使用
  • 原文地址:https://www.cnblogs.com/nasa/p/1223455.html
Copyright © 2011-2022 走看看