Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no nextDouble(), nextFloat() and nextLong() which accept a bound in java.util.Random

Tags:

java

random

I was reading java.util.Random class and noticed that there is no nextDouble(), nextFloat() and nextLong() which can accept a bound.

There are many way to get it done like this.

But my question is why java did not provide us with these required method like nextInt(int n) which accept the bound.

Is there any specific reason they did not provide these methods?

like image 444
Mehraj Malik Avatar asked Dec 06 '25 10:12

Mehraj Malik


1 Answers

A good API always tries to provide the essential elements that a user needs to do his job.

Having nextInt(int n) is just one possible implementation. What if you need other distributions?!

In other words: the Random API could try to anticipate all potential usage patterns, but that would very much bloat the whole API. Instead, the designers choice a very small interface - but you got all the elements required to build your own things on top of that.

Thing is: in the end, this is a design style decision by the people who created the Random class. And as so often, problems could be solved in many different ways. Thus you shouldn't draw deep conclusions on the solution that was picked here.

like image 162
GhostCat Avatar answered Dec 08 '25 00:12

GhostCat



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!