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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Feb 28, 2008, 11:32 AM
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);