请编写一个函数模拟n次扔两个骰子(可能的点数1-6),返回两个骰子点数之和为10的次数占总次数的比例,然后编写一个主函数调用该函数3次,分别传入n值为1000、10000、100000,依次打印函数返回的比例值。
时间: 2023-03-12 11:03:00 浏览: 86
答:可以使用Python来编写该函数,具体代码如下:def roll_dice(n):
count_10 = 0
for i in range(n):
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
sum_dice = dice1 + dice2
if(sum_dice == 10):
count_10 += 1
return count_10 / ndef main():
for n in [1000, 10000, 100000]:
rate = roll_dice(n)
print("当n为%d时,两个骰子点数之和为10的次数占总次数的比例为%f" % (n, rate))if __name__ == "__main__":
main()