MpcLib.Common.StochasticUtils.SafeRandom.Next C# (CSharp) Метод

Next() публичный Метод

public Next ( System.Numerics.BigInteger maxValue ) : System.Numerics.BigInteger
maxValue System.Numerics.BigInteger
Результат System.Numerics.BigInteger
        public BigInteger Next(BigInteger maxValue)
        {
            int bits = (int) Math.Ceiling(BigInteger.Log(maxValue, 2));
            int bytes = bits / 8 + ((bits % 8 == 0) ? 0 : 1);

            int bitsToZero = (bits % 8 == 0) ? 0 : (8 - bits % 8);

            byte[] data = new byte[bytes];
            while (true)
            {
                rand.NextBytes(data);
                for (int i = 0; i < bitsToZero; i++)
                {
                    data[data.Length - 1] &= (byte) ~(1 << (8 - i));
                }

                BigInteger ret = new BigInteger(data);
                if (ret < maxValue)
                    return ret;
            }
        }

Same methods

SafeRandom::Next ( int minValue, int maxValue ) : int