Skip to content

Instantly share code, notes, and snippets.

@falkreon
Last active August 22, 2017 17:25
Show Gist options
  • Save falkreon/28bf8bed26a9d766b100c1b0cfa06c09 to your computer and use it in GitHub Desktop.
Save falkreon/28bf8bed26a9d766b100c1b0cfa06c09 to your computer and use it in GitHub Desktop.
Selecting unusually-distributed pseudorandom numbers
/**
* Creates a random number from (-1..1), exclusive, distributed in an inverse-bell-curve fashion. That is, numbers
* closer to -1 or 1 are exponentially more likely to appear than numbers closer to 0.
*/
public static double invertedNormalRandom(Random r) {
/*
* Implementation note: log10 reaches y=0 at x=1, and reaches y=1 at x=10, so it's really important, if we
* want to get good numbers out of it, to feed it numbers in the range of 1..10. So we multiply by 9 and add 1.
*/
double a = Math.log10((r.nextDouble()*9)+1);
return (r.nextBoolean()) ? a : -a;
}
@falkreon
Copy link
Author

Also, if you're wondering why I don't just use sine, it creates a thoroughly unpleasant distribution, squished hard to the edges and with a spike in the middle due to the high slope near the center. Sometimes the transformations needed to create a given distribution are extremely unintuitive; even gaussianRandom itself seems to have an anomalous spike at zero which does not fit its uniform distribution claim(!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment