MrAntunes
3/15/2017 - 6:31 PM

generate real random ints

generate real random ints

//generates random integers between an upper and lower bound
        ////for SMS code [246584 ~ 814567]
        public int GenRandInt(int min, int max)
        {
            RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider();
            uint scale = uint.MaxValue;

            while (scale == uint.MaxValue)
            {
                // Get four random bytes
                byte[] four_bytes = new byte[4];
                Rand.GetBytes(four_bytes);

                // Convert that into an uint
                scale = BitConverter.ToUInt32(four_bytes, 0);
            }

            // Add min to the scaled difference between max and min.
            return (int)(min + (max - min) * (scale / (double)uint.MaxValue));
        }