Reputation: 19
import random
rainDropX = random.randint(5, WIDTH - 5)
rainDrop1 = Circle(2, MIDNIGHT_BLUE)
for i in range(75):
pause(WAIT)
add(rainDrop1, rainDropX, -5)
if rainDrop1.getY() >= HEIGHT:
rainDrop1.setLocation(rainDropX, -5)
In this portion, rainDrop1 will jump from (randomDropX, -5) to the bottom; it will then respawn at (randomDropX, -5). Currently, randomDropX will store the same random integer. How can I produce a new random integer and store it in the same variable efficiently?
Upvotes: 1
Views: 149
Reputation: 18792
Whenever you need the random value, make a new call to random.randint()
at the time you need it, rather than once at the beginning of the script (which will simply get the result and assign it to the name rainDropX
that one time)
It could be an optimization to create a table of random data and iterate over it, (either at runtime or once and save it) which is famously done in DOOM (src), though I doubt it practically matters unless you're requesting a huge amount of random data (which you may be for rain!).
Here's some you could use!
>>> [randint(0, 23) for _ in range(1024)]
[4, 3, 19, 12, 13, 20, 15, 18, 20, 12, 6, 8, 11, 4, 4, 7, 13, 22, 8, 22, 1, 7, 7, 5, 10, 13, 17, 9, 16, 19, 16, 21, 1, 23, 13, 5, 19, 16, 17, 14, 11, 10, 19, 7, 21, 19, 21, 8, 13, 13, 15, 9, 11, 16, 15, 18, 18, 21, 2, 0, 1, 1, 13, 3, 23, 1, 11, 14, 20, 8, 6, 2, 2, 7, 14, 6, 16, 6, 9, 6, 19, 0, 3, 0, 1, 16, 17, 23, 22, 21, 22, 8, 3, 10, 10, 2, 12, 0, 13, 6, 22, 15, 3, 17, 11, 7, 14, 0, 3, 15, 5, 4, 18, 1, 13, 11, 4, 23, 3, 4, 4, 15, 12, 1, 12, 0, 2, 23, 12, 16, 23, 21, 19, 0, 22, 8, 10, 14, 4, 16, 21, 16, 16, 6, 6, 13, 17, 9, 8, 8, 15, 7, 3, 6, 15, 0, 15, 14, 7, 0, 3, 20, 5, 4, 2, 12, 2, 12, 15, 9, 19, 5, 11, 10, 1, 18, 16, 11, 14, 11, 23, 10, 8, 8, 4, 13, 15, 8, 11, 22, 19, 20, 20, 10, 14, 20, 13, 14, 8, 16, 22, 18, 17, 20, 9, 14, 9, 5, 19, 22, 12, 19, 6, 1, 20, 1, 2, 22, 6, 12, 20, 12, 19, 21, 7, 16, 16, 11, 17, 2, 16, 9, 5, 11, 6, 7, 15, 16, 22, 8, 15, 5, 13, 11, 3, 23, 17, 0, 4, 21, 2, 21, 12, 18, 21, 12, 22, 1, 15, 9, 22, 16, 11, 20, 14, 1, 2, 12, 2, 23, 0, 21, 21, 13, 19, 14, 11, 23, 20, 1, 11, 11, 19, 4, 8, 14, 2, 23, 9, 12, 5, 16, 6, 11, 19, 3, 21, 8, 16, 0, 5, 17, 0, 11, 15, 2, 8, 9, 4, 15, 2, 21, 20, 0, 10, 15, 6, 2, 19, 6, 14, 13, 6, 18, 9, 3, 6, 8, 23, 17, 4, 3, 9, 12, 15, 1, 3, 13, 7, 0, 12, 10, 0, 15, 16, 3, 7, 6, 22, 5, 3, 18, 21, 23, 9, 12, 12, 15, 16, 6, 16, 20, 14, 1, 23, 2, 8, 18, 5, 9, 6, 20, 12, 13, 9, 2, 16, 16, 12, 10, 13, 16, 5, 13, 9, 17, 3, 23, 9, 2, 12, 20, 2, 11, 6, 21, 22, 23, 19, 17, 1, 0, 21, 0, 1, 15, 6, 2, 11, 5, 22, 12, 22, 22, 20, 17, 15, 22, 1, 3, 14, 7, 1, 13, 16, 5, 11, 1, 10, 17, 22, 15, 18, 4, 9, 1, 11, 11, 5, 9, 5, 18, 14, 4, 17, 16, 12, 7, 20, 7, 16, 20, 4, 1, 8, 13, 2, 6, 12, 17, 22, 11, 13, 17, 14, 7, 1, 11, 9, 7, 10, 3, 2, 14, 16, 8, 18, 22, 19, 17, 12, 9, 8, 0, 6, 17, 11, 14, 7, 0, 14, 13, 18, 4, 7, 16, 12, 9, 10, 11, 20, 2, 2, 6, 21, 13, 15, 7, 17, 12, 2, 6, 4, 13, 15, 2, 21, 19, 19, 6, 11, 22, 2, 17, 15, 19, 12, 7, 23, 11, 16, 4, 10, 18, 2, 17, 13, 17, 17, 7, 11, 13, 14, 6, 3, 18, 0, 0, 6, 12, 23, 18, 16, 1, 13, 4, 23, 15, 17, 11, 7, 4, 11, 10, 4, 12, 6, 16, 18, 12, 9, 9, 16, 0, 3, 0, 0, 23, 20, 4, 16, 9, 20, 5, 14, 9, 13, 12, 23, 19, 5, 14, 1, 12, 23, 12, 23, 15, 16, 13, 10, 23, 15, 14, 14, 21, 12, 3, 19, 13, 11, 7, 8, 15, 6, 12, 21, 4, 10, 2, 22, 4, 0, 4, 21, 8, 23, 12, 6, 16, 19, 14, 3, 1, 9, 1, 3, 6, 23, 3, 11, 5, 0, 10, 22, 13, 19, 14, 5, 4, 12, 11, 18, 23, 2, 0, 18, 1, 16, 8, 0, 8, 18, 0, 1, 1, 0, 1, 21, 2, 7, 2, 21, 14, 11, 17, 4, 23, 17, 1, 2, 2, 17, 15, 16, 20, 3, 9, 17, 19, 18, 20, 8, 15, 5, 14, 17, 8, 9, 14, 7, 17, 13, 20, 16, 21, 7, 18, 0, 13, 10, 4, 23, 23, 9, 23, 4, 12, 2, 16, 6, 1, 9, 23, 9, 19, 0, 4, 0, 0, 14, 15, 15, 23, 15, 17, 15, 17, 6, 7, 2, 8, 9, 17, 12, 8, 20, 1, 22, 22, 8, 4, 16, 22, 16, 2, 15, 21, 20, 14, 17, 9, 21, 3, 4, 4, 13, 16, 23, 18, 11, 6, 17, 8, 6, 16, 17, 18, 17, 0, 14, 17, 13, 20, 14, 23, 22, 18, 19, 5, 11, 11, 2, 5, 2, 15, 16, 3, 7, 20, 23, 13, 17, 7, 15, 2, 13, 16, 20, 19, 6, 6, 16, 4, 8, 1, 5, 9, 17, 21, 8, 8, 11, 15, 1, 10, 20, 15, 1, 1, 23, 21, 18, 3, 11, 4, 13, 10, 10, 7, 18, 16, 15, 23, 2, 2, 0, 3, 18, 8, 3, 18, 16, 12, 17, 13, 16, 2, 18, 20, 12, 14, 17, 8, 21, 6, 22, 19, 0, 10, 19, 11, 9, 15, 10, 22, 13, 21, 22, 2, 8, 5, 19, 14, 2, 22, 7, 11, 9, 15, 3, 0, 7, 20, 14, 9, 8, 1, 20, 12, 19, 21, 1, 11, 16, 3, 12, 13, 12, 7, 10, 8, 3, 22, 20, 20, 12, 12, 15, 22, 19, 19, 19, 0, 4, 8, 19, 1, 12, 22, 8, 23, 23, 23, 4, 0, 20, 8, 4, 19, 15, 6, 12, 16, 22, 4, 8, 17, 14, 10, 2, 20, 7, 10, 17, 13, 10, 12, 4, 4, 1, 20, 7, 21, 9, 12, 4, 8, 19, 7, 22, 23, 11, 15, 14, 22, 6, 2, 6, 13, 4, 0, 11, 12, 11, 6, 17, 7, 14, 12, 17, 8, 3, 20, 4, 12, 3, 17, 16, 12, 14, 3, 10, 6, 21, 0, 17, 20, 3, 9, 0, 7, 4, 21, 21, 17, 9, 19, 18, 23, 5, 23, 15, 20]
Here's an iterable class you could use
class StaticRand():
def __init__(self, value_min, value_max, length=1024):
self._length = length
self._min = value_min
self._max = value_max
# make a new table of random data
self.table = [randint(self._min, self._max) for _ in range(self._length)]
self.index = 0
def __iter__(self):
return self
def __next__(self):
try:
return self.table[self.index]
finally:
self.index = (self.index + 1) % self._length
example usage
>>> from itertools import islice
>>> r = StaticRand(0, 23)
>>> list(islice(r, 5))
[22, 3, 22, 15, 17]
>>> a = list(islice(r, 1024))
>>> b = list(islice(r, 1024)) # wraps around all the way!
>>> c = list(islice(r, 1023)) # a little short
>>> a == b
True
>>> a == c
False
Upvotes: 1