dimanche 25 novembre 2018

How to get a random float value between two floats?

The desired output is a random float between two floats. This is the way I did it but this method doesn't work for negative floats like: random float between -1f and -30f since the bound has to be positive and I get IllegalArgumentException. It also looks pretty complicated...if you have an easier approach that would be lovely. Cheers!

unitsConsumed = rnd.nextInt(Math.round(maxUnitsConsumed-minUnitsConsumed))+minUnitsConsumed;

Where rnd is an instance of Random.




Aucun commentaire:

Enregistrer un commentaire