Take the 2-minute tour ×

I've been racking my brain all morning trying to come up with the following algorithm, this is especially frustrating because I'm sure that it's possible.

What I need is a class that has a function that returns boolean. It can be called any number of times and will return true XX% of the time. This CANNOT be a random distribution, for example:

If the ratio X is set to 0.6 and the function is called 100 times, we need to return exactly 60 true results. In which order "left overs" are used doesn't matter, for example: if the function was called 99 times it would be OK to return either 59 or 60 true values.

The trick here is that the ratio needs to be variable.

For some setup, I'm working in a multi threaded environment so I'm keeping my "hitNumber" variable in an AtomicLong in order to avoid synchronization issues.


share|improve this question
what have you tried so far? –  andy mccullough Sep 30 '13 at 15:15
What is this boolean result testing for? Or is it just returning a semi-randomized result? –  StormeHawke Sep 30 '13 at 15:15
Fill a boolean array with X true values. Shuffle. Retrieve in order. –  Geobits Sep 30 '13 at 15:16
hah. I was just typing out what @Geobits just recommended, but he beat me to it –  StormeHawke Sep 30 '13 at 15:17
@MattKlooster Ah, then go with that. I read it as "a non-random distribution in a random order". –  Geobits Sep 30 '13 at 15:26

5 Answers 5

up vote 9 down vote accepted

If all you want is to maintain the overall percentage, just keep track of the percentage so far (probably as an explicit rational), and return true if you're under the target percentage, or false if you're over it.

share|improve this answer

Your criterion that it can't be random is pretty ill-defined. I suppose you mean that the quantity T/(T+F) is as close to the ratio as integer T and F will allow.

So you'll end up with something like this:

class TrueFalseGenerator {

  final double ratio;
  long nTrue, nFalse;

  TrueFalseGenerator(double ratio) {
    this.ratio = ratio;
    nTrue = nFalse = 0;

  synchronized boolean next() {
    long den = nTrue + nFalse;
    if (den == 0 || (double)nTrue / den < ratio) {
      return true;
    } else {
      return false;
share|improve this answer

To build on Ben's answer, you can maintain static class variables to keep track of past function calls. Something like:

bool myFunc( float true_percentage ) {

  count++; // where count and count_true are class static variables initialized to zero.

  if ( float( count_true ) / count >= true_percentage )
    return false;

  return true;

share|improve this answer
This looks like C/C++ code a lot more than Java. ;) Java doesn't support local static variables. It'd be better to store the two counts as non-static instance variables. –  yshavit Sep 30 '13 at 15:27
He did tag this java... –  Gene Sep 30 '13 at 15:30
Yes sorry, missed the tag. I have changed them to class static variables. –  user1990169 Sep 30 '13 at 15:52

This version uses only integer arithmetic and doesn't need any counter:

public class Distribution {
    private int numerator;
    private int denominator;
    private int error;

    public Distribution(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;

    public synchronized boolean next() {
        error += numerator;
        if (error >= denominator) {
            error %= denominator;
            return true;

        return false;


Distribution dist = new Distribution(6, 10); // 6 trues out of 10
dist.next(); // get next bool
share|improve this answer
"doesn't need any counter" you're using a counter –  Cruncher Sep 30 '13 at 15:36
No, I'm just calculating an error value that never gets greater than denominator+numerator-1. –  isnot2bad Sep 30 '13 at 15:40
to stay as close to the numerator/denominator ratio as possible after every call, the first next() should return true here, but it's false. –  Cruncher Sep 30 '13 at 15:45
//1st call randomize(x) from main with x as percentage
//this calls fischershuffle to shuffle the boolean array
//den calls to return bool are randomized with x trues and 100-x falses per 100 calls

class A{
public static int count=0;
public static boolean fill[]=new boolean[100];

public static void randomize(double x)
double totaltrue=x*100;
double totalfalse=100-totaltrue;




static boolean fischershuffle(boolean[] ar)
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
      int index = rnd.nextInt(i + 1);
      boolean a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    return ar;

     public static boolean retunbool()
     return fill[count];
     count=0;//resets after 100 for next 100 calls
share|improve this answer
expanded on geobits answer –  Kaushik Sivakumar Sep 30 '13 at 15:41
Just to let you know, my comment was based on an incorrect assumption, so this answer doesn't really solve the problem in the way it was intended. –  Geobits Sep 30 '13 at 15:42

Your Answer


