Random boolean with weight or bias

11,449

Solution 1

I was wondering if there is a better or more natural way of doing this.

The approach you're using already is fine.

* As far as I know, there's not a standard Java method that will make this code any shorter.


* For non-cryptographic purposes.

Solution 2

1) Yes, i think your approach is valid and I don't see another easier way.

2) There is a library for handling random numbers of different statistical distributions:

http://introcs.cs.princeton.edu/java/22library/StdRandom.java.html

Solution 3

Here's what I'm using. Very similar to FracturedRetina's answer.

Random random = new Random();

// 20% chance
boolean true20 = (random.nextInt(5) == 0) ? true : false;

// 25% chance
boolean true25 = (random.nextInt(4) == 0) ? true : false;

// 40% chance
boolean true40 = (random.nextInt(5) < 2) ? true : false;

Solution 4

    public boolean getBiasedRandom(int bias) {
      int c;
      Random t = new Random();
     // random integers in [0, 100]
      c=t.nextInt(100);
      if (c>bias){return false;
      }
      else{return true;}
      }

This one is based on percentage...

Solution 5

The MockNeat library implements this feature.

Example for generating a boolean value that has 99.99% of being true:

MockNeat m = MockNeat.threadLocal();
boolean almostAlwaysTrue = m.bools().probability(99.99).val();
Share:
11,449

Related videos on Youtube

diestl
Author by

diestl

Updated on June 13, 2022

Comments

  • diestl
    diestl almost 2 years

    I need to generate some random booleans. However I need to be able to specify the probability of returning true. As a results doing:

    private Random random = new Random();
    random.nextBoolean();
    

    will not work.

    One possible solution would be:

    private Random random = new Random()
    
    public boolean getRandomBoolean(float p){
        return random.nextFloat() < p;
    }
    

    I was wondering if there is a better or more natural way of doing this.

    EDIT: I guess I am asking whether there is a library class that provides a nextBoolean(float probability) method.

    • Jon Skeet
      Jon Skeet almost 11 years
      What sort of "better" are you looking for? That looks reasonable to me...
    • Oliver Charlesworth
      Oliver Charlesworth almost 11 years
      These are two (essentially) separate problems, so should be asked in two separate posts...
    • diestl
      diestl almost 11 years
      @JonSkeet I guess I was hoping for something along the lines of Random.nextBoolean(long probability)
    • Duncan Jones
      Duncan Jones almost 11 years
      I've snipped out your second question, please post it separately if you want an answer for it. I've also voted to close this question, as it is primarily opinion-based.
    • diestl
      diestl almost 11 years
      Thanks. I'll edit the question so it is less opinion based.
  • Oliver Charlesworth
    Oliver Charlesworth almost 11 years
    @Baadshah: Hmm, yes. I've reworded now!
  • Oneiros
    Oneiros over 6 years
    The validation is reasonable, the rest makes the code unnecessarily less readable. Keep it simple
  • Oneiros
    Oneiros over 6 years
    The ternary operator is totally redundant. Why not just boolean true20 = (random.nextInt(5) == 0) ?
  • bobanahalf
    bobanahalf over 6 years
    You know how when you learn a new thing, it's useful in every situation? I completely agree that it's redundant.