Пример #1
0
        public static bool GetRandomBoolean(this IRandomNumberGenerator generator)
        {
            var buf = new byte[1];      // PERF: pre-allocate or pool or cache the buffers.

            generator.FillWithRandomBytes(buf);
            return(buf[0] >= 128);       // Low values of the byte are false, high values are true.
        }
Пример #2
0
        // Implementation for GetRandom...() based on http://codereview.stackexchange.com/questions/6304/algorithm-to-convert-random-bytes-to-integers
        #region GetRandom[U]Int32()
        public static uint GetRandomUInt32(this IRandomNumberGenerator generator)
        {
            var buf = new byte[4];      // PERF: pre-allocate or pool or cache the buffers.

            generator.FillWithRandomBytes(buf);
            var i = BitConverter.ToUInt32(buf, 0);

            return(i);
        }
Пример #3
0
        private static byte[] GetRandomBytesLarge(this IRandomNumberGenerator generator, int byteCount)
        {
            var result = new byte[byteCount];
            int offset = 0;

            while (offset < byteCount)
            {
                var remaining          = byteCount - offset;
                var bytesToGetThisTime = generator.MaxRequestBytes < remaining ? generator.MaxRequestBytes : remaining;
                generator.FillWithRandomBytes(result, offset, bytesToGetThisTime);

                offset = offset + bytesToGetThisTime;
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Generates the requested number of random bytes.
        /// </summary>
        /// <param name="generator"></param>
        /// <param name="byteCount"></param>
        /// <returns></returns>
        public static byte[] GetRandomBytes(this IRandomNumberGenerator generator, int byteCount)
        {
            // PERF: pre-allocate or pool or cache the buffers.
            if (byteCount > generator.MaxRequestBytes)
            {
                // We need to loop for a larger request.
                // For greater chance of inlining, we push the more complex logic to a separate method.
                return(GetRandomBytesLarge(generator, byteCount));
            }

            var result = new byte[byteCount];

            generator.FillWithRandomBytes(result);
            return(result);
        }
Пример #5
0
        public static decimal GetRandomDecimal(this IRandomNumberGenerator generator)
        {
            // Generate 96 bits of randomness for the decimal (which is its full precision).
            // However, 96 bits will sometimes exceed the required range of 0.0-1.0
            // Masking off the top bit (giving 95 bits precision) gives a distribution of 0.0-0.25.
            // And we multiply by 4 to spread that in the range 0.0-1.0 nicely.
            // Note that the .038m part is based on looking at the final distribution and fitting to right up to the top.
            // PERF: no loops! Just a bitwise and and decimal multiply.
            // PERF: cache / reuse the byte array.

            var rawBytes = new byte[12];

            generator.FillWithRandomBytes(rawBytes);

            var lo  = BitConverter.ToInt32(rawBytes, 0);
            var mid = BitConverter.ToInt32(rawBytes, 4);
            var hi  = BitConverter.ToInt32(rawBytes, 8) & 0x7ffffff;
            var d   = new decimal(lo, mid, hi, false, 28);

            d = d * 4.038m;
            return(d);
        }