Generate Random Boolean Probability

后端 未结 7 1707
离开以前
离开以前 2021-02-05 01:17

I only know how I can generate a random boolean value (true/false). The default probability is 50:50

But how can I generate a true false value with my own probability? L

7条回答
  •  心在旅途
    2021-02-05 01:39

    Well, one way is Random.Next(100) <= 20 ? true : false, using the integer value of NextInt to force your own probability. I can't speak to the true 'randomness' of this method though.

    More detailed example:

    Random gen = new Random();
    int prob = gen.Next(100);
    return prob <= 20;
    

提交回复
热议问题