Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate a random number between [1, n] using a random generator that generates a floating point number between [0.0, 1.0)

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].

like image 890
shreyasva Avatar asked Nov 01 '11 15:11

shreyasva


People also ask

How do you generate a random number between 0 and 1?

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.

How do you generate a random float between 0 and 1 in Python?

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.

How do you generate a random float number?

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.

Which function random module is used to generate a random float number between 0 and 1?

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).


1 Answers

Multiply by n, take the floor, and add 1.

like image 86
jason Avatar answered Oct 03 '22 01:10

jason