Here you can find the source of randomProbability(double probability)
Parameter | Description |
---|---|
probability | - probability double >= 0 and <= 1 which is the probability of true |
public static boolean randomProbability(double probability)
//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); } }