首页 > 解决方案 > 如何创建一个伪随机数生成器程序,既可以充当魔术 8 球,又可以从一副牌中选择一张牌?

问题描述

正如标题所说,我想知道如何创建一个伪随机数生成器程序,该程序既可以充当魔术 8 球,又可以从一副牌中挑选一张牌。这是我目前拥有的魔术 8 球的代码:

response = ["As I see it, yes", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't count on it", "It is certain", "It is decidedly so", "Most likely", "My reply is no", "My sources say no", "Outlook not so good", "Outlook good", "Reply hazy, try again", "Signs point to yes", "Very doubtful", "Without a doubt", "Yes", "Yes - definitely", "You may rely on it"]

import math

#Constants
N = 10000 # The norm
A = 4875  # The adder
M = 8601  # The multiplier

K = 1
# -----------------------------------------
# The pseudorandom number generator

keep_going = 'Y'

while keep_going in ['y', 'Y']: #Allows user to input either a lowercase or uppercase 'Y' when asked if they want to ask another question
    X = input("Enter a YES or NO question: ")
    S = int(input("Now enter an integer: "))
    for i in range(K):
        S = (S * M + A) % N # Random Number Generator
        r = S/N #On the interval [0,1)
        magic = math.floor(20 *r)
        print("The Magic 8 Ball says:", response[magic]) 

        #Asking the user if they want to ask another question
        keep_going = input('Do you want to ask another question ' +
                           '(Enter Y for yes and N for no): ')

我的想法是,如果用户输入“n”,我希望程序然后询问用户是否愿意从一副纸牌中挑选随机纸牌,而不是像魔术 8 球程序那样行事,而是就像一个程序,例如从 52 个可能的选项中选择 5 张随机卡片。理想情况下,我希望“选卡程序”采用相同的伪随机数生成器布局。

标签: pythonspyder

解决方案


推荐阅读