Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# probability and random numbers

I've seen this question asked in a number of ways, but I just need a sanity check of what I'm doing here.

Basically I want to trigger an event with a probability of 25% based on a random number generated between 1 and 100 using:

int rand = random.Next(1,100);

Will the following achieve this?

if (rand<=25)
{
    // Some event...
}

I thought I would use a number between 1 and 100 so I can tweak the probabilities later on - e.g. adjust to 23% by using

if (rand<=23) {...}

Thanks for taking a look.

like image 777
CdrTomalak Avatar asked Apr 09 '12 21:04

CdrTomalak


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

The biggest error you are making is it should be random.Next(0,100) as the documentation states

minValue: The inclusive lower bound of the random number returned.

maxValue: The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

Emphisis mine, exclusive means it does not include number you passed in, so my code generates the range 0-99 and your code generates the range 1-99.

So change your code to

int rand = random.Next(0,100)

if (rand < 25) //25%
{
    // Some event...
}

//other code
if (rand < 23) //23%
{
    // Some event...
}

The change from <= to < is because you are now using the exclusive upper bounds range

like image 196
Scott Chamberlain Avatar answered Sep 28 '22 04:09

Scott Chamberlain


The second argument of Next(int, int) is the exclusive upper bound of the desired range of results. You should therefore use this:

if (random.Next(0, 100) < 25)

or, if you must use 1-based logic,

if (random.Next(1, 101) <= 25)
like image 23
phoog Avatar answered Sep 28 '22 04:09

phoog