ABDUL MANAN
ABDUL MANAN

Reputation: 81

true false quiz in c#

I'm building a auto guiz generator in C# in which take the pdf file from user and generate MCQS Fill in the blanks and true false. I completed the two modules MCQS and Fill in the blanks but have the problem in generating true-false values in a random way.

So my problem is: how can i randomly generate true and false values?

Upvotes: 0

Views: 3017

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

public bool GetRandomBoolean(Random rnd)
{
    return rnd.Next(0, 2) == 0;
}

http://msdn.microsoft.com/en-us/library/system.random.next

Edit: Note that you should not use this method in this way:

for(int i = 0; i < 1000; i++)
{
    bool randomBool = GetRandomBoolean(new Random());
}

That would generate always the same "random" boolean since it's seeded with the same time. Instead you should reuse the random instance, f.e. in this way:

var rnd = new Random();
for(int i = 0; i < 1000; i++)
{
    bool randomBool = GetRandomBoolean(rnd);
}

Upvotes: 8

Related Questions