+ 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
6 Antworten
+ 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
+ 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
+ 2
Thank you so much. so yes this is just to sub 1 from the range but why we need to use it?
+ 2
Sorry i dont understand your question you dont need the -1 ? i dont know why you need the len of x?
+ 2
@Shabnam Ah now i get your question haha
+ 2
hahhah you had right. It was my mistake asking something without any background. Thank you dear @Chrizzhigh ❤