Bain
Bain

Reputation: 844

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).

Upvotes: 1

Views: 11403

Answers (4)

Sam
Sam

Reputation: 31

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.)

Upvotes: 0

Mayur Patel
Mayur Patel

Reputation: 1025

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()

Upvotes: 0

Day
Day

Reputation: 9693

I'm not sure why you want to do this, but here's how to compute many random numbers, throwing most of them away, and then using the last one after 3 seconds have elapsed.

import random
import time

start = time.clock()
while time.clock() - start < 3:
    random_number = random.randint(0,100)

print random_number

This pointlessly throws away about 2 million perfectly good random numbers on my machine.

(And, as abarnert points out, this also maxes out one CPU core for the whole 3 seconds in a busy loop, which is very, very wasteful, but I thinks it's what you were asking for?)

EDIT: Updated to use time.clock instead of time.time, as suggested by abarnert again (thanks), because this seems to give better resolution across platforms and doesn't suffer from problems when the system time is altered in the middle of the program running.

Upvotes: 1

abarnert
abarnert

Reputation: 365657

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.

Upvotes: 1

Related Questions