I want to generate a list of random integer within a range:
from random import randint
lower = 0
upper = 20
length = 10
random_array = [randint(lower, upper) for _ in range(length)]
I don't think it is elegant and I come up with another solution:
random_array = [randint(lower, upper)] * length
But it generate a list of identical integers.
It might be that randint
is executed before list
.
I try putting off randint
with lambda
:
random_array = [(lambda: randint(lower, upper))()] * length
but fail.
So I want to know if it's possible to delay the evaluation of items (randint
in this case) and then have it triggered once the list is assembled?