- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 python 的新手,我想知道如何使代码重复 random.randint
部分 100 次。
#head's or tail's
print("you filp a coin it lands on...")
import random
heads = 0
tails = 0
head_tail =random.randint(1, 2,)
if head_tail == 1:
print("\nThe coin landed on heads")
else:
print("\nThe coin landed on tails")
if head_tail == 1:
heads += 1
else:
tails += 1
flip = 0
while True :
flip +=1
if flip > 100:
break
print("""\nThe coin has been fliped 100 times
it landed on heads""", heads, """times and tails""", tails,
"""times""")
input("Press the enter key to exit")
最佳答案
您可以通过列表理解在一行中完成所有操作:
flips = [random.randint(1, 2) for i in range(100)]
然后像这样计算正面/反面的数量:
heads = flips.count(1)
tails = flips.count(2)
或者更好的是:
num_flips = 100
flips = [random.randint(0, 1) for _ in xrange(num_flips)]
heads = sum(flips)
tails = num_flips - heads
关于 python ,重复random.randint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707182/
我有一个关于两种方法的简单而广泛的问题: scipy.stats.randint 和 numpy.random.randint 在阅读了这两种方法的 API 之后,我对何时最好使用每种方法感到有点困惑
在Python编程中,随机数的生成是一项常见的任务。random.randint()函数是Python中生成随机整数的常用函数之一。该函数可以生成指定范围内的随机整数,为数据处理、模拟实验和游戏开发等
对于这个项目,我需要在随机点绘制 400 个不同的正方形,并且彼此之间至少相距 5 个像素。目前,框大多绘制在窗口底部,并且在程序卡住之前只绘制了大约一半。我咨询了我的教授,他们被难住了。 #Impo
from random import randint number_of_trials = 100 range_of_random_numbers = 1000 for each in number_
from random import randint 这是我用来导入 Random 模块的代码。当我运行代码时,它会导入一个名为 random.py 的用于测试代码的文件。这导致了各种错误,所以我在回
我是 python 的新手,我想知道如何使代码重复 random.randint 部分 100 次。 #head's or tail's print("you filp a coin it lands
我想在 a 和 b 之间选择一个随机整数,包括在内。 我知道 3 种方法。然而,他们的表现似乎非常违反直觉: import timeit t1 = timeit.timeit("n=random.ra
我正在用 Python 中的随机库来模拟我工作的项目,我发现自己处于一个非常奇怪的位置。 假设我们在 Python 中有以下代码: from random import randint import
我想在 a 之间选择一个随机整数和b (两者均包含),统计权重为c . c是 a 之间的值和b . 应用权重因子最有效的方法是 c到 random.randint? 我得到的最接近的是this问题,但
我想在 a 之间选择一个随机整数和b (两者均包含),统计权重为c . c是 a 之间的值和b . 应用权重因子最有效的方法是 c到 random.randint? 我得到的最接近的是this问题,但
我有一个生成sms_token的函数。它不能与数据库中现有的重复。然而,token的空间可能不够大,那么新的token可能会发生冲突。 Python 3.7.0 from random import
我正在努力在 Python 2.7 中导入random。我想让这段代码生成两张随机卡。当前代码不会生成随机卡。如果我请求卡片 (9,11),代码会生成总共 19 张卡片 (9,J)。输出没有任何随机性
我正在尝试用 python 编写简单的角色创建页面。我想要重新滚动统计信息的选项。问题是,刷新页面后,统计数据保持不变。下面是我的代码: View .py from my import Charac
我正在尝试运行一个简单的程序,其中我试图在循环中运行 random.randint() 以更新字典值,但它似乎无法正常工作。它似乎总是产生相同的值(value)。 到目前为止的程序如下。我正在尝试创建
我目前正在尝试在添加下一个项目之前将一个随机项目(使用 randint)从一个列表附加到另一个列表。好吧,这可能是一种奇怪的表达方式,但我不确定如何描述它 - 我很抱歉。因此,这里是我当前的相关代码和
在 python 控制台中我可以运行: from random import randint 但是当我运行时出现错误: import random.randint 错误说: ModuleNotFoun
我有这个游戏,您需要对列表中发送的 5 个随机表情符号使用react。问题在于,有时 random.randint() 会吐出相同的表情符号两次,因此不可能用相同的表情符号对同一消息做出两次 reac
我想生成 0-9 之间的随机整数(包括两端),但我想确保它不会经常连续生成相同的数字。我计划使用 random 模块中的 randint 函数。但我不确定它是否会派上用场。 random.randin
所以我生成了一个带有 numpy 命名高度的随机列表... heights = random.randint(10, size=random.randint(20)) 我将此列表用于测试目的,但我有一
我有一个名为:cars 的专栏并想创建另一个名为 persons 的使用 random.randint()我有: dat['persons']=np.random.randint(1,5,len(da
我是一名优秀的程序员,十分优秀!