0

I'm creating a Java application that needs to randomly generate numbers with probabilities. These float numbers (or doubles doesn't change much) must be from 0 to 100 where 0 and 100 have the lowest probability of coming out while 50 (which is the middle) is the one with the highest probability... practically, moving away from the center the rarity that comes out that number is always greater until it becomes almost impossible. For example the number 99.9 comes out 1 time in 5 billion, but as I said it is just an example so the rarity of the numbers must be established by the function. Basically I would like to say that the closer you get to 100 or 0, the rarity tends to infinity.

However, I would like it to be a function with a min parameter and a max parameter to make it more versatile.

(Sorry if the question is not very clear but i'm not native and i'm still learning english...)

  • 1
    It doesn't sound like you have strict requirements for something like a normal distribution. But since other people do, most solutions you'll find will be aimed at that. For example, [see this post.](https://stackoverflow.com/questions/75677/converting-a-uniform-distribution-to-a-normal-distribution#3265174) These solutions might involve more math than you are comfortable with, however. If so, you'll need to be more clear about your requirements. You mention a probability at the extreme (99.9), but you really need to define the whole curve from 0 to 100. – erickson Feb 09 '22 at 17:44

1 Answers1

1

Perhaps you could use Random's nextGaussian() method which generates a random number based on the default mean of 0.0 and standard deviation of 1.0. In your case, I believe the mean would be 50, and you could calculate the standard deviation so that it fits your requirements. You could use this link: Java normal distribution in order to help answer your question.

Docs for Random.nextGaussian(). I would also suggest looking into normal distributions because I believe the match what you are asking for.

Hope that helped!

TheSj
  • 376
  • 1
  • 11