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

前端 未结 6 869
迷失自我
迷失自我 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:16

    You could, but you should do it this way:

    double test = random.NextDouble();
    

    If you wanted to get random integer ( 0 or 1), you should set upper bound to 2, because it is exclusive

    int test = random.Next(0, 2);
    

提交回复
热议问题