Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        上个月关闭。
                    
                
        

因此,我试图生成硬币翻转游戏代码。

到目前为止,我已经像这样用python编写了代码(以50%的概率卡在生成函数中)

import random

coin = random.randint(0, 1)

print(coin)
if coin == 1:
    print("front")
else:
    print("back")


从上周开始我就开始学习python!我需要帮助
另外,如果有人也可以通过Java解决我的问题吗?

最佳答案

给定概率,您可以有10次时间(我为50%50%)

import numpy as np
import collections
elements = [1, 0]
probabilities = [0.5, 0.5]
a=np.random.choice(elements, 10, p=probabilities)


结果将是:
[1, 1, 0, 1, 0, 1, 1, 1, 1, 0]

您可以通过以下代码计算0和1的数量:

collections.Counter(a)

计数的结果是:Counter({1: 7, 0: 3})

合而为一:

import numpy as np
import collections
elements = [1, 0]
probabilities = [0.5, 0.5]
a=np.random.choice(elements, 10, p=probabilities)
print(a)
count=collections.Counter(a)
print(count)

10-07 18:13