+ 3

Randint (0, len (x)-1)

I wonder what exactly that (-1) does? I know that randint provide a random among the numbers we have. here for example we have a list of numbers and we are going to selece one of them by random. so range is between the lower number to the highest. so what is that -1 for? Thank you in advance for any help

7th Oct 2017, 9:35 AM
Shabnam
Shabnam - avatar
6 Respuestas
+ 3
yes dear Chrizzhigh you are true. I started to ask something from the middle. I found that coz "list" are zero index based while "len" start counting at 1. so for being true at my code i needed to sub 1 from len to make it equal to index
7th Oct 2017, 12:56 PM
Shabnam
Shabnam - avatar
+ 2
"len(x) - 1 " is an expression which will be evaluated and the result will be used as upper bound for the random number. eg. x = [1 , 2, 3, 4] randint (0, len (x)-1) this will evaluate to randint(0, 3) the -1 does nothing special it only subs 1 from the length of the list. hope this helps
7th Oct 2017, 11:05 AM
Chrizzhigh
Chrizzhigh - avatar
+ 2
Thank you so much. so yes this is just to sub 1 from the range but why we need to use it?
7th Oct 2017, 11:22 AM
Shabnam
Shabnam - avatar
+ 2
Sorry i dont understand your question you dont need the -1 ? i dont know why you need the len of x?
7th Oct 2017, 12:10 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
@Shabnam Ah now i get your question haha
7th Oct 2017, 1:24 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
hahhah you had right. It was my mistake asking something without any background. Thank you dear @Chrizzhigh ❤
7th Oct 2017, 1:25 PM
Shabnam
Shabnam - avatar