Suppose you have a random number generator that generates a random floating point number between [0.0, 1.0) such as drand48
, how can you create a random number generator that generates an integer between [1, n].
uniform() function. The random. uniform() function is perfectly suited to generate a random number between the numbers 0 and 1, as it is utilized to return a random floating-point number between two given numbers specified as the parameters for the function.
Generate a random float number between 0 to 1Use a random. random() function of a random module to generate a random float number uniformly in the semi-open range [0.0, 1.0) . Note: A random() function can only provide float numbers between 0.1. to 1.0.
In order to generate Random float type numbers in Java, we use the nextFloat() method of the java. util. Random class. This returns the next random float value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.
Almost all module functions depend on the basic function random() , which generates a random float uniformly in the semi-open range [0.0, 1.0).
Multiply by n
, take the floor, and add 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With