samedi 10 décembre 2022

Python generating a deterministic random number based on user input

I've been messing around with chatgpt and wrote a function that generates a seemingly random answer, my important condition was that it needed to be output the same number when the equation was entered again, the ultimate liar:

import hashlib
import random

# Define constants for the minimum and maximum values of the
# random number function
RANDOM_NUMBER_MIN = 0
RANDOM_NUMBER_MAX = 99_999

# Define the random_number function
def random_number(input_string):
  # Create a hash object and update it with the input string
  hash_object = hashlib.sha256()
  hash_object.update(input_string.encode('utf-8'))

  # Get the hexadecimal representation of the hash
  hex_hash = hash_object.hexdigest()

  # Convert the hexadecimal hash to an integer and use it to seed the
  # random number generator
  random.seed(int(hex_hash, 16))

  # Generate and return a random number in the range
  # defined by the RANDOM_NUMBER_MIN and RANDOM_NUMBER_MAX constants
  return random.randint(RANDOM_NUMBER_MIN, RANDOM_NUMBER_MAX)

# Define the main function
def main():
  # Prompt the user to enter an equation
  equation = input("Enter an equation (e.g. 2 + 2): ")

  # Print the result of the random number function
  print(f"The result of the random number function is: {random_number(equation)}")

# Call the main function
if __name__ == "__main__":
  main()

Example:

Enter an equation (e.g. 2 + 2): 1 + 1
The result of the random number function is: 68959

I then generated a function that brute forces 10 correct answers.

...
# Define constants for the minimum and maximum values of the
# random number function and the number of solutions to print
RANDOM_NUMBER_MIN = 0
RANDOM_NUMBER_MAX = 99_999
SOLUTION_COUNT = 10
...
# Define the main function
def main():
  # Generate all combinations of numbers from RANDOM_NUMBER_MIN to RANDOM_NUMBER_MAX
  # where the result is between RANDOM_NUMBER_MIN and RANDOM_NUMBER_MAX
  found_solutions = 0
  for result in range(RANDOM_NUMBER_MIN, RANDOM_NUMBER_MAX + 1):
    # Generate all combinations of numbers from RANDOM_NUMBER_MIN to result
    # where the result is equal to result
    for num1 in range(RANDOM_NUMBER_MIN, result + 1):
      num2 = result - num1
      equation = f"{num1} + {num2}"
      if random_number(equation) == result:
        print(f"{equation} = {result}")
        found_solutions += 1
        if found_solutions == SOLUTION_COUNT:
          return

  if found_solutions < SOLUTION_COUNT:
    print(f"Only found {found_solutions} solutions out of {SOLUTION_COUNT}.")
...

Output:

411 + 32 = 443
63 + 436 = 499
382 + 289 = 671
424 + 247 = 671
491 + 232 = 723
855 + 63 = 918
898 + 99 = 997
119 + 928 = 1047
1031 + 393 = 1424
566 + 887 = 1453

Creating this took forever (I could've probably written this quicker), I constantly had to correct it, but maybe you've suggestions on how to improve this?

You could for example evaluate the expression and then add or subtract a number greater than 0, but that wouldn't be safe.




Aucun commentaire:

Enregistrer un commentaire