+ 4
difference between randint and randrange
i dont understand what is the main difference in practice between random.randint and random randrange? with both i can choose the same interval, right?
3 Respostas
+ 6
With randint(start,stop) you get an integer between start and stop.
With randrange(start,stop,step) you can get an integer between start and stop by step
eg:
randint(2,7) = 2,3,4,5,6,7
randrange(0,10,2) = 0,2,4,6,8,10
+ 1
oh, super. clear now! thanks.
+ 1
1.randint() takes only 2 arguments
Used when we know the start and end values.
Syntax-randint(start,stop)
Example
import random
random.randint(10) #invalid
random.randint(1,10) #valid
2.randrange() can take 1,2 or 3 argument
Used when we don't know both the limit
Syntax -randrange(start,stop,steps)
Example-
import random
random.randrange(5)
random.randrange(1,9)
random.randrange(1,10,2)