182days

Random Number (Search & Timer)

May 10th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. #Takes a number from the user and then cycles through 10,000 random numbers to find it.
  2. #Uses a timer to attempt a benchmark
  3.  
  4. usernum = input("What number would you like to find?") #collects a number from the user
  5. from datetime import datetime
  6. start_time = datetime.now() #Starts the timer
  7. count = 0 #set guess counter to 0
  8. while True:
  9.     import random
  10.     rnum = random.randint(0,9999) #choose a random number between a lower and higher limit
  11.     count +=1 #add one to the guess counter
  12.     print (rnum) #display the number currently being guessed on screen
  13.     if int (usernum) == int (rnum): #if there is a match between random number and user number
  14.         end_time = datetime.now() #Ends the timer
  15.         print("random number was found!")
  16.         duration = (end_time - start_time) #calculates the time between the start and end of guessing
  17.         print("Time taken to find guess your number was", (round(duration.total_seconds())), "seconds")#Displays the time taken
  18.         count2 = str(duration.total_seconds()) #converts the duration into a string
  19.         count3 = (float(count)/float(count2)) #establishes how many guesses per second
  20.         print("Your CPU guessed", (round(count3,2)), "times per second.") #Displays the number of guesses per second the CPU managed
  21.         break
Add Comment
Please, Sign In to add comment