How do I generate random number between 0 and 1 in C#?

前端 未结 6 863
迷失自我
迷失自我 2020-12-10 11:55

I want to get the random number between 1 and 0. However, I\'m getting 0 every single time. Can someone explain me the reason why I and getting 0 all the time? This is the

6条回答
  •  庸人自扰
    2020-12-10 12:17

    You are getting zero because Random.Next(a,b) returns number in range [a, b), which is greater than or equal to a, and less than b.

    If you want to get one of the {0, 1}, you should use:

    var random = new Random();
    var test = random.Next(0, 2);
    

提交回复
热议问题