Having a function that generates only 0 or 1, write a function that using the above function to generate integer in the range [0, n] with n given, and by giving same chance to all the numbers by calling only that function.
Anonymous
/** *This is NlogN solution on idea if it is good or bad **/ public class BinaryProbability { public static void main(String[] args) { for (int i = 0 ; i n){ result[i] = nextRandom % n; }else{ result[i] = nextRandom; } } return result; } static Random random = new Random(); public static int zeroOrOne(){ return random.nextBoolean() ? 1 : 0; } }
Check out your Company Bowl for anonymous work chats.