samedi 17 décembre 2016

Java Random with probability specified rules

In my case I'm picking maximum ten numbers using this method (let's call them T):

private static int getProcessingTime() {
    int upperBound = random.nextInt(200);
    if (upperBound <= 100)
        return 1;
    if ((upperBound -= 100) <= 99)
        return upperBound / 11;
    return 10;
}

Example output:

1,1,3,4,1,9,1,1,1,3

Next step is that end-point user picks number from, let's say 0.9 to 1.2(let's call it result condition - RC).

Now here's where things get interesting. For every number from first randomizing I must pick (quite randomly, too) one number (let's call it P) to fulfill following rules:

  • Every P must be greater than T

  • Sum of (T/P) must match the RC range.

First condition is pretty straight forward but I can't see how to accomplish second condition.

I was thinking about picking P simply from Random.nextInt(/*bound here*/), checking if second condition is fulfilled and then, if it's not, incrementing/decrementing each of P numbers, but this seems to be not so elegant solution.

What am I missing? Is there any sort of algorithm to accomplish second condition that I don't know nothing about?

Already checked SO answers, but didn't find this sort of issue.




Aucun commentaire:

Enregistrer un commentaire