Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random boolean

Tags:

random

vb.net

I am a little stuck with a simple Question: how to generate a simple random Boolean?
If am correct, a Boolean 0 = false and 1 = true, but how to suse that?

Current code:

Dim RandGen As New Random
Dim RandBool As Boolean
RandBool = Boolean.Parse(RandGen.Next(0, 1).tostring)
like image 433
Christian Sauer Avatar asked Mar 10 '13 17:03

Christian Sauer


2 Answers

Or just simply:

Random rng = new Random();
bool randomBool = rng.Next(0, 2) > 0;

Saves some processing power of parsing text, whereas a simple compare is enough.

Edit: Second parameter is exclusive, so should be .Next(0, 2).

like image 92
Caramiriel Avatar answered Sep 22 '22 08:09

Caramiriel


Dim RandGen As New Random

    Dim RandBool As Boolean

    RandBool = RandGen.Next(0, 2).ToString

    TextBox1.Text = RandBool
like image 37
Technikminer Avatar answered Sep 22 '22 08:09

Technikminer