Advertisement
MichalDK

How frequently is 7 in randomly generated code?

Aug 10th, 2022
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. from random import choice
  2. from datetime import datetime
  3. import sys
  4.  
  5. with open("77777.txt", "a") as f:  # open file with write access
  6.     # Saving the reference of the standard output
  7.     original_stdout = sys.stdout
  8.  
  9.     sys.stdout = f
  10.  
  11.     print(70 * "*")
  12.     print("How frequently will randomly generated code contain digit 7 ?")
  13.     print(70 * "*")
  14.  
  15.     number_of_digits = 5
  16.     print(f"Number of digits: {number_of_digits}")
  17.     values = [_ for _ in range(1, 9)]
  18.     print(f"Possible values: {values}")
  19.     counter_of_luck = [0 for _ in range(0, number_of_digits + 1)]
  20.  
  21.     generated_codes = 333333
  22.     print(f"Number of generated codes: {generated_codes}")
  23.     print(70 * "*")
  24.     for secret_code_index in range(0, generated_codes):
  25.         secret_code = ""
  26.         for a in range(0, number_of_digits):
  27.             secret_code += str(choice(values))
  28.         seven = [digit for digit in secret_code if digit == "7"]
  29.         counter_of_luck[len(seven)] += 1
  30.  
  31.     for lucky_pointer in range(0, len(counter_of_luck)):
  32.         print(
  33.             f"Number of 7 :{lucky_pointer}. Number of codes: {counter_of_luck[lucky_pointer]}.   "
  34.             f"It is {counter_of_luck[lucky_pointer]*100/generated_codes} % cases."
  35.         )
  36.  
  37.     # Reset the standard output
  38.     sys.stdout = original_stdout
  39.  
  40.     print("This message will be written to the screen.")
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement