How should I generate a random number from 0 to 2? BCL Random is not random

How should I generate a random number between 0 and up to 2?

I am currently using the closed static Randomon Random object and calling _random.Next (0, 2), BUT the values ​​are not random ... I see the sequences 0,0,0,1,1,0,0, 0, 1, 1,1

The following is clearly not random. How am I supposed to do this?

Thank you so much

+3
source share
4 answers

If you mean that you need random numbers 0 or 1, then what you are doing is already right.

If you want the numbers 0, 1 or 2 , you need to write _random.Next(0, 3).

+10
source

( ).

, ( ), ( ).

, , , 1 .

"0, 1, 2", random.Next(0, 3). 0 1, .

, Random , , . - Random , .

+3

Random , . :

Random _random = new Random(DateTime.UtcNow.Millisecond);

, Next . , Next(0, 2) 0 1 ( , , ).

+1

MaxValue " ". 2, _random.Next(0,3)

+1

Source: https://habr.com/ru/post/1795203/


All Articles