简体   繁体   中英

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?

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, fe in this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM