Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Distribution Algorithm

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.

Thanks!

like image 556
Matt Klooster Avatar asked Feb 21 '26 09:02

Matt Klooster


1 Answers

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.

like image 199
Sneftel Avatar answered Feb 22 '26 23:02

Sneftel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!