'Compute random number over certain time interval with Python

I did some research before posting but seem to be at a lost (not too experienced in coding).

I am attempting to generate or compute a random number for certain time interval with Python. I'm not looking for full code, I want help using the time library if that is the correct one to use.

Pseudo-code:

Allow python [PC] to compute a random number for 3 seconds

------> Store the computed generation in a value (i can handle this)

I would then use the random generated value to link access a python list (which would be automatically generated via a random number generation as well but i can figure that out).



Solution 1:[1]

First, you didn't say what kind of random number you want to generate, but given that your example is 10, I assume it's an integer in some range—let's say you're calling random.randrange(30).

Now, you want to compute a number every second for 3 seconds, then keep the last one. I don't know why you'd even want to do this, but you can do it like this:

for i in range(3):
    number = random.randrange(30)
    time.sleep(1.0)

At the end of 3 seconds, number will be the third random number generated.

The key here is that, to do something once per second (in a synchronous program—don't do this in a GUI or server!)—you just call time.sleep.

If the operation you were doing took a significant chunk of a second (or longer), this wouldn't be appropriate. Instead, you'd want to compute the start time, and sleep until a second after that:

t0 = time.monotonic()
for i in range(3):
    number = random.randrange(30)
    t0 += 1
    time.sleep(t0 - time.monotonic())

Note that I've used time.monotonic here. This function is specifically designed for this kind of use case. It returns as much precision as can be gotten with reasonable efficiency (in particular, unlike time.time, it doesn't give you 1s precision on some platforms), and it guarantees that you'll never go backward even if, e.g., you change the system clock in the middle of the program. If you're using 3.2 or earlier, either look through the docs for the best alternative (possibly using time.clock()), or look into using ctypes to call the appropriate platform native function.

But in this case, random.randrange is going to take somewhere on the order of a microsecond, which is so much less time than the minimum resolution of most systems' simple timers that there's no reason to do such a thing.

Solution 2:[2]

If you want to take 3 seconds to get a random number, because you're concerned about the quality of the random number, you can use os.urandom() to generate the value. If all you really want to do is to select an item from your list at random, you can use random.choice()

Solution 3:[3]

Note: The function time.clock() has been removed, after having been deprecated since Python 3.3: use time.perf_counter() or time.process_time() instead, depending on your requirements, to have well-defined behavior. (Contributed by Matthias Bussonnier in bpo-36895.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mayur Patel
Solution 3 Sam