Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number: 0 or 1

Tags:

c#

random

Am I looking too far to see something as simple as pick a number: 0 or 1?

        Random rand = new Random();          if (rand.NextDouble() == 0)         {             lnkEvents.CssClass = "selected";         }         else         {             lnkNews.CssClass = "selected";         } 
like image 213
Rickjaah Avatar asked Sep 29 '09 14:09

Rickjaah


People also ask

How can a random number be 0 or 1?

int n = rand() % 1 + 0; will produce 0 always as rand() % 1 gives 0 ( rand()%a generates number between 0 to a-1 ).

Does random random () include 0 and 1?

random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

Does random random include 1?

A random number generator always returns a value between 0 and 1, but never equal to one or the other.

How do you select a random number from 0 to 1 in Python?

random() is what you are looking for: From python docs: random. random() Return the next random floating point number in the range [0.0, 1.0).


2 Answers

Random rand = new Random();  if (rand.Next(0, 2) == 0)     lnkEvents.CssClass = "selected"; else     lnkNews.CssClass = "selected"; 

Random.Next picks a random integer between the lower bound (inclusive) and the upper bound (exclusive).

like image 128
JDunkerley Avatar answered Sep 22 '22 08:09

JDunkerley


If you want 50/50 probability, I suggest:

Random rand = new Random();  if (rand.NextDouble() >= 0.5)     lnkEvents.CssClass = "selected"; else     lnkNews.CssClass = "selected"; 
like image 22
Mitch Wheat Avatar answered Sep 20 '22 08:09

Mitch Wheat