0

i would like to generate random numbers for 5 minutes. after that, i would like to know the most common numbers.

my question is:

I am not sure about how much time I need. Can i MANUALLY terminate the script at 1 minute or 1 minute 19 seconds and get the result

my code is as follows:

import random
from collections import Counter
t_end = time.time() + 60*5
numbers = []
while time.time() < t_end:
    number=''.join(map(str, [random.randint(0,9)for value in range(0,4)]))
    numbers.append(number)

print(Counter(numbers))
  • 1
    why do you want to use a time period and not a range of iterations? – luigigi Nov 06 '19 at 12:56
  • Are you trying to demonstrate `randint`'s distribution is uniform? – Pynchia Nov 06 '19 at 13:04
  • Quite a convoluted way to test the distriubution. `numbers` grows around 150k items/s. This is close to 50mio items for 5min. Tried counting with a regular dictionary? And is the weird random-expression on purpose? Using just `randint(0,9999)` is 4 times faster... – Peter Schneider Nov 06 '19 at 14:31

3 Answers3

1

There are a myriad number of options:

  • print an intermediate result every 5s or so and abort the script with ctrl+C
  • check if space or something else is pressed, if so, abort the loop (see here for how to)
  • listen to signals and handle accordingly (this could also gracefully terminate the script for option 1)
  • open a socket and listen to a socket if a stop signal comes in
  • create a gui with TKinter or what and add a stop button
  • ...

The first one is the easiest but also the most quick&dirty one. For the second option, the following script should get you started (your script was working for me after adding whitespaces and linebreaks):

import keyboard
while True: 
    if keyboard.read_key() == "p": 
        print("You pressed p") 
        break

Assuming python3 and windows: Note that keyboard is not shipped with python on default. You need to install it with pip3 install keyboard. Pip3.exe might not be on your path. cd to where you installed python and look around. For me it was in the Scripts-folder (my installation is pretty messed up though).

Peter Schneider
  • 1,683
  • 12
  • 31
  • mind to tell me the first method? can you illustrate it? i pressed ctrl+c but could not have any result. – pythonbeginner Nov 06 '19 at 14:44
  • if possible can tell me the second method? i used the following code but I could not get the result as well import keyboard while True: if keyboard.read_key() == "p": print("You pressed p") break – pythonbeginner Nov 06 '19 at 14:45
  • @pythonbeginner Updated answer, your script was working for me after adding linebreaks. Did you install the package? For the right python-installation? For the first: I run the scripts directly in console. If you use an IDE there thould be a stop button or something somewhere to abort the currently running script (ctrl+C does not do much more on console). – Peter Schneider Nov 06 '19 at 15:17
  • hi, i run the scripts in console, yes, the method 1 works – pythonbeginner Nov 07 '19 at 01:23
  • for method 2, i have failed to get the result. this is my code: import random import keyboard from collections import Counter import time t_end = time.time() + 60*5 numbers = [] while time.time() < t_end: number=''.join(map(str, [random.randint(0,9)for value in range(0,4)])) numbers.append(number) if keyboard.read_key() == "p": print("You pressed p") break else: continue print(Counter(numbers)) – pythonbeginner Nov 07 '19 at 01:27
  • i download the code in .py format. after that i run the code on command prompt. then i type pyton abc.py. after a while i press ctrl+C, i have keyboard interrupt and cannot get any result. – pythonbeginner Nov 07 '19 at 01:51
  • @pythonbeginner see https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python (second answer looks easier to set up) – Peter Schneider Nov 07 '19 at 12:53
0
import random
from collections import Counter
import time
t_end = time.time() + 60*5
numbers = []
while time.time() < t_end:
    number=''.join(map(str, [random.randint(0,9)for value in range(0,4)]))
    numbers.append(number)
    if time.time()==(t_end-60*4): # break a 1 min
        break
print(Counter(numbers))
5x shanks
  • 116
  • 6
0

As luigigi already mentioned in the comment, you could simply use the number of iterations as a limit, and use a for-loop:

number_of_iterations = 10000

for i in range(number_of_iterations):
    place_your_function_here()

However, if you for some reason really want to use the time, here is how you can do it: Update the current time at the bottom of your while block, and in the continuing condition of the while loop, compare it to the finish time:

import datetime


delta_in_seconds = 60
finish = datetime.datetime.now() + datetime.timedelta(0, delta_in_seconds)
while time < finish:
    place_your_function_here()
    time = datetime.datetime.now()
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44