Q. The random() function generates a random floating point number in the range 0.0 to 1.0 and randint(a, b) function generates random integer between range a to b. To generate random numbers between range a to b using random(), following formula is used:


math.random()*(b-a) + a

Now if we have following two statements (carefully have a look)


(i) int ( (math.random()* (b-a) + a))

(ii) math.randint (a, b)


Can we say above two statements are now producing random integers from the same range ? Why?


Answer :-

No, their range is not the same.

The first statement will be able to produce random integers (say N) in the range a <= N<b whereas the second statement will be producing random integers in the range a <= N <= b.

The reason being random() function excludes the upper limit while generating random numbers whereas radint() includes both upper limit and lower limit.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post