Create a random boolean. - CSharp System

CSharp examples for System:Random

Description

Create a random boolean.

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;//from   w  w  w . j  a  v  a2 s .  c o m

public class Main{
        /// <summary>
        /// Create a random boolean.
        /// </summary>
        /// <returns>random boolean</returns>
        public static bool GenerateRandomBoolean()
        {
            // Create a byte array as a data source
            var rnd = SeedRandom();
            return rnd.Next(0, 2) == 0;
        }
        private static Random SeedRandom()
        {
            return new Random(Guid.NewGuid().GetHashCode());
        }
}

Related Tutorials