python - 如何创建一个伪随机数生成器程序,既可以充当魔术 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 张随机卡片。理想情况下,我希望“选卡程序”采用相同的伪随机数生成器布局。
解决方案
推荐阅读
- php - 使用 Laravel 在列表中显示用户名列表
- r - 转换为日期时处理月份缩写
- python - React Native Websocket 到 Python Socketserver 通信
- powerbi - Power BI:查找使用操作 ID(事件顺序)和客户 ID 选择的最后一个值
- python - 如何在 Python 中输入转义序列?
- laravel - 不能在 DB::select 的 Laravel 中为 IN 条件使用参数
- python - UnboundLocalError:分配前引用的局部变量“检查”
- shopify - Shopify 行项目变体在单独的行中
- angular - 如何在表单上的用户输入上自动添加小数点
- amazon-web-services - 使用 boto3 将数据上传到 DynamoDB 的最佳方式?