Write a python program that writes next number probability based on list of previous…
- User
Write a python program that writes next number probability based on list of previous numbers guessing that all numbers is distributed like discrete uniform distribution between 0 and 36. Use scipy for that. Lower probabilities of more frequent numbers
- Assistant
Here is a possible solution:
pythonimport numpy as np from scipy.stats import rv_discrete # Define the discrete uniform distribution between 0 and 36 xk = np.arange(37) pk = np.full(37, 1/37) # equal probabilities for all numbers # Initialize the random variable with the distribution rv = rv_discrete(values=(xk, pk)) # Input the previous numbers as a list (e.g. [10, 22, 15]) prev_nums = [10, 22, 15] # Count the frequency of each number in the list freqs = np.bincount(prev_nums, minlength=37) # Adjust the probabilities to favor less frequent numbers pk = (pk + freqs) / (len(prev_nums) + 37) # Reinitialize the random variable with the adjusted distribution rv = rv_discrete(values=(xk, pk)) # Generate the next number according to the adjusted distribution next_num = rv.rvs() print(f"The next number is {next_num}")