0

I'm sure many of you have heard of the game called crash. Some popular examples of this are satoshi, bustabit.

It's pretty complicated to explain, so I will begin assuming that you are familiar with it.

Anyways, my goal is to recreate something along these lines. I want to make a function that will give me an exponential distribution double PRNG between 1 and 100.

Basically, a random double between 1 and 100 in which the probability of getting a double near 1 is much more likely than getting a double closer to 100.

I've been experimenting with a few things so far, but I really just can't figure this one out. Any insight as to how this could be achieved would be greatly appreciated.

Peter_Browning
  • 249
  • 1
  • 2
  • 10
  • 1
    Do these answer your question? [Pseudorandom Number Generator - Exponential Distribution](https://stackoverflow.com/questions/2106503/pseudorandom-number-generator-exponential-distribution) or [JAVA - exponential distribution](https://stackoverflow.com/questions/29020652/java-exponential-distribution) – Welbog Jul 13 '20 at 17:45
  • I looked at those earlier, but my implementation seems to be flawed-- `double c = Math.random(); double prng = Math.log(1-c)/(-1.5);` outputs doubles between 0 and 3 for some reason – Peter_Browning Jul 13 '20 at 17:49
  • That should be outputting doubles from 0 to infinity. You can add an offset `+1` to make it from 1 to infinity, and retry any results greater than 100 to ensure your distribution is between 1 and 100. – Welbog Jul 13 '20 at 17:52

1 Answers1

0

You can achieve this using a bias function.

public static void main(final String[] args) {
    double sum = 0;
    for (int i = 0; i < 100; i++) {
        final double x = Math.random();
        sum += biasFunction(x, 0.7);
    }
    System.out.println(sum);
}

public static double biasFunction(final double x, final double bias) {
    // Adjust input to make control feel more linear
    final double f = Math.pow(1 - bias, 3);
    return (x * f) / (x * f - x + 1);
}

The more you increase the bias, the more your result will go against zero. You could also just multiply the result of the function by 100 instead of taking an average.

Take a look at this visualization.

Daschi
  • 1
  • 1
  • 3