Is there any way to randomise boolean values using the Random method, It's no problem with integers but I need to randomise booleans??
Random RandomClass = new Random();
int x = RandomClass.Next(32, 64);
Thanks in Advance
Roy.
Is there any way to randomise boolean values using the Random method, It's no problem with integers but I need to randomise booleans??
Random RandomClass = new Random();
int x = RandomClass.Next(32, 64);
Thanks in Advance
Roy.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Feb 28, 2008, 6:32 AM EST
Hi Roy,
To produce random booleans,I'd get a random integer of either 0 or 1 and then map the result to false or true respectively. Something like this:
using System;
class Test
{
static Random rand = new Random();
static void Main()
{
// produce 20 random booleans
bool b;
for (int i = 0; i < 20; i++)
{
b = (rand.Next(0,2) == 1);
Console.WriteLine(b);
}
Console.ReadLine();
}
}
If, for some reason, you want the booleans to be based on random nmbers between 32 and 63, I'd map even numbers to false and odd numbers to true:
b = (rand.Next(32,64) % 2 == 1);