Java Random randomProbability(double probability)

Here you can find the source of randomProbability(double probability)

Description

{ method randomProbability returns true with probabiliy given by its input

License

Apache License

Parameter

Parameter Description
probability - probability double >= 0 and <= 1 which is the probability of true

Return

- true with probability - probability }

Declaration

public static boolean randomProbability(double probability) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static final Random gRandomizer = new Random(System.currentTimeMillis());

    /**{ method//from   w ww  . jav  a 2 s  .  co  m
     * randomProbability returns true with probabiliy given by its input
     *   @function - choose an element of an array at random
     *  @param probability - probability double >= 0 and <= 1 which is the probability of true
     *   @return - true with probability - probability
     }*/
    public static boolean randomProbability(double probability) {
        if (probability > 1 || probability < 0)
            throw new IllegalArgumentException("randomProbability must take a 0 <= number <= 1");
        if (probability < 0)
            throw new IllegalArgumentException("randomProbability must take a number > 0");
        double test = gRandomizer.nextDouble();
        return (test < probability);
    }
}

Related

  1. randomNick()
  2. randomPassword()
  3. randomPermutation(int size)
  4. randomPermutations(int[] tab, Random r)
  5. randomPermute(List l, Random rand)
  6. randomPseudo()
  7. randomRange(int end)
  8. randomScalingFactor()
  9. randomSeed()