Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个骰子卷转换为两个骰子卷_Python - Fatal编程技术网

Python 将一个骰子卷转换为两个骰子卷

Python 将一个骰子卷转换为两个骰子卷,python,Python,我是python的新手,现在我的代码可以刺激1000次滚动一个骰子,但是我需要一些改进,使其能够刺激1000次滚动两个骰子 以下是迄今为止我所拥有的功能,它运行良好,只需要一些改进: import random test_data = [0, 0, 0, 0, 0, 0] n = 1000 for i in range(n): result = random.randint(1, 6) test_data[result - 1] = test_data[result - 1] + 1

我是python的新手,现在我的代码可以刺激1000次滚动一个骰子,但是我需要一些改进,使其能够刺激1000次滚动两个骰子

以下是迄今为止我所拥有的功能,它运行良好,只需要一些改进:

import random
test_data = [0, 0, 0, 0, 0, 0]
n = 1000
for i in range(n):
  result = random.randint(1, 6)
  test_data[result - 1] = test_data[result - 1] + 1

for i in range(0, 6):
  print ("Number of ", i+1, "'s: ", test_data[i])
关于如何掷两个骰子并获得与我的代码现在所做的类似的输出的任何建议如下:

Number of  1 's:  180

Number of  2 's:  161

Number of  3 's:  179

Number of  4 's:  159

Number of  5 's:  146

Number of  6 's:  175

在这种情况下,结果是2到12之间的数字。不过,为了简单起见,最好保持第一个索引

因此,测试数据列表需要增加到存储12项,因此我们应该调用random.randint1,6两次而不是两次,并将它们相加:

import random

test_data = [0] * 12
n = 1000
for i in range(n):
  # adding up two dices
  result = random.randint(1, 6) + random.randint(1, 6)
  test_data[result - 1] += 1

for i, x in enumerate(test_data, 1):
  print ("Number of ", i, "'s: ", x)

在这里写+=1比写=…+更优雅1,因为这里我们避免了两次写入测试数据[result-1]。此外,在Python中,通常直接枚举集合,而不是索引。可以使用EnumerateRable、start_index生成一个由2元组i、n和i组成的iterable,其中i是索引,x是集合中与该索引相关的元素。

在这种情况下,结果是2到12之间的数字。不过,为了简单起见,最好保持第一个索引

因此,测试数据列表需要增加到存储12项,因此我们应该调用random.randint1,6两次而不是两次,并将它们相加:

import random

test_data = [0] * 12
n = 1000
for i in range(n):
  # adding up two dices
  result = random.randint(1, 6) + random.randint(1, 6)
  test_data[result - 1] += 1

for i, x in enumerate(test_data, 1):
  print ("Number of ", i, "'s: ", x)
在这里写+=1比写=…+更优雅1,因为这里我们避免了两次写入测试数据[result-1]。此外,在Python中,通常直接枚举集合,而不是索引。可以使用EnumerateRable、start_index生成一个由2元组i、n和i组成的iterable,其中i是索引,x是集合中与该索引相关的元素。

您可以使用numpy,此解决方案允许您指定任意数量的骰子:

import numpy as np

no_of_dice = 2
sides_on_die = 6
rolls = 1000
dice = np.array([0]*rolls)

for i in range(no_of_dice):
    dice += np.random.randint(1,sides_on_die+1,rolls)
data = np.bincount(dice)

for i in range(no_of_dice,no_of_dice*sides_on_die+1):
    print ("Number of ", i, "'s: ", data[i])
收益率:

Number of  2 's:  26
Number of  3 's:  55
Number of  4 's:  100
Number of  5 's:  106
Number of  6 's:  139
Number of  7 's:  152
Number of  8 's:  135
Number of  9 's:  104
Number of  10 's:  87
Number of  11 's:  64
Number of  12 's:  32
您可以使用numpy,此解决方案允许您指定任意数量的骰子:

import numpy as np

no_of_dice = 2
sides_on_die = 6
rolls = 1000
dice = np.array([0]*rolls)

for i in range(no_of_dice):
    dice += np.random.randint(1,sides_on_die+1,rolls)
data = np.bincount(dice)

for i in range(no_of_dice,no_of_dice*sides_on_die+1):
    print ("Number of ", i, "'s: ", data[i])
收益率:

Number of  2 's:  26
Number of  3 's:  55
Number of  4 's:  100
Number of  5 's:  106
Number of  6 's:  139
Number of  7 's:  152
Number of  8 's:  135
Number of  9 's:  104
Number of  10 's:  87
Number of  11 's:  64
Number of  12 's:  32

如果允许您使用其他python模块,那么您可以利用random进行计数。从random.randint切换到,您可以同时掷两个骰子:

import random
from collections import Counter


def roll_n_dice_of_sides_x_times(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields 'x' values that 
    hold the sum of the 'x' dice rolls.""" 
    r = range(1,sides+1)
    yield from (sum(random.choices(r,k=n)) for _ in range(x))

# this does allthe counting and dice throwingof 1000 2-6sided-dice-sums
c = Counter(roll_n_dice_of_sides_x_times(2,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for eyes,count in sorted(c.items()):
    print(f"Number of {eyes:>3}'s : {count}")
输出:

Number of   2's : 24
Number of   3's : 51
Number of   4's : 66
Number of   5's : 115
Number of   6's : 149
Number of   7's : 182
Number of   8's : 153
Number of   9's : 116
Number of  10's : 68
Number of  11's : 58
Number of  12's : 18
Doku:

它是一个字典-你给它一个iterable,int计算iterable的每个元素出现的频率: 如果您想要单骰子结果,可以修改代码,使其不求和骰子,而是在生成随机数时传递元组。我对它们进行了分类,使5,4,5与4,5,5的投掷相同:

import random
from collections import Counter

def roll_n_dice_of_sides_x_times_no_sum(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields a sorted tuple 
    of the dice throwsof all  'x' dice rolls.""" 
    r = range(1,sides+1)

    # instead of summing, create a tuple (hashable, important for Counter)
    # and return that sorted, so that 4,4,5 == 5,4,4 == 4,5,4 throw:
    yield from ( tuple(sorted(random.choices(r,k=n))) for _ in range(x))

# throw 3 6-sided dice 1000 times and count:
c = Counter(roll_n_dice_of_sides_x_times_no_sum(3,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for dice,count in sorted(c.items()):
    print(f"{dice} occured {count} times")
产量缩短:

(1, 1, 1) occured 3 times
(1, 1, 2) occured 14 times
[...] 
(2, 3, 5) occured 32 times
(2, 3, 4) occured 21 times
[...]
(4, 6, 6) occured 10 times
(5, 5, 5) occured 3 times
(5, 5, 6) occured 20 times
(5, 6, 6) occured 9 times
(6, 6, 6) occured 4 times

如果允许您使用其他python模块,那么您可以利用random进行计数。从random.randint切换到,您可以同时掷两个骰子:

import random
from collections import Counter


def roll_n_dice_of_sides_x_times(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields 'x' values that 
    hold the sum of the 'x' dice rolls.""" 
    r = range(1,sides+1)
    yield from (sum(random.choices(r,k=n)) for _ in range(x))

# this does allthe counting and dice throwingof 1000 2-6sided-dice-sums
c = Counter(roll_n_dice_of_sides_x_times(2,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for eyes,count in sorted(c.items()):
    print(f"Number of {eyes:>3}'s : {count}")
输出:

Number of   2's : 24
Number of   3's : 51
Number of   4's : 66
Number of   5's : 115
Number of   6's : 149
Number of   7's : 182
Number of   8's : 153
Number of   9's : 116
Number of  10's : 68
Number of  11's : 58
Number of  12's : 18
Doku:

它是一个字典-你给它一个iterable,int计算iterable的每个元素出现的频率: 如果您想要单骰子结果,可以修改代码,使其不求和骰子,而是在生成随机数时传递元组。我对它们进行了分类,使5,4,5与4,5,5的投掷相同:

import random
from collections import Counter

def roll_n_dice_of_sides_x_times_no_sum(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields a sorted tuple 
    of the dice throwsof all  'x' dice rolls.""" 
    r = range(1,sides+1)

    # instead of summing, create a tuple (hashable, important for Counter)
    # and return that sorted, so that 4,4,5 == 5,4,4 == 4,5,4 throw:
    yield from ( tuple(sorted(random.choices(r,k=n))) for _ in range(x))

# throw 3 6-sided dice 1000 times and count:
c = Counter(roll_n_dice_of_sides_x_times_no_sum(3,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for dice,count in sorted(c.items()):
    print(f"{dice} occured {count} times")
产量缩短:

(1, 1, 1) occured 3 times
(1, 1, 2) occured 14 times
[...] 
(2, 3, 5) occured 32 times
(2, 3, 4) occured 21 times
[...]
(4, 6, 6) occured 10 times
(5, 5, 5) occured 3 times
(5, 5, 6) occured 20 times
(5, 6, 6) occured 9 times
(6, 6, 6) occured 4 times

这是两个无法区分的骰子的解决方案,这意味着投掷1和3的处理方式与投掷3和1的处理方式相同。在这种方法中,我们使用dict而不是list,因为对于两个或更多的用户!无法区分的骰子列表中会有像3,1这样的洞组合,这是永远不会出现的,因为我们将它们视为1,3

import random

counts = {}
for _ in range(1000):
    dice = tuple(sorted([random.randint(1, 6), random.randint(1, 6)]))
    counts[dice] = counts.get(dice, 0) + 1
骰子现在是两个骰子,经过排序,3,1被视为1,3,并从一个列表转换成一个元组,基本上是一个不可变的列表,所以我们可以使用它作为字典计数的键。然后我们只是增加骰子组合的数量

与列表不同的是,字典没有排序,但我们确实希望按照骰子显示的内容进行排序,因此我们按照keys=dice进行排序:

for dice in sorted(counts.keys()):
    print("{} occurred {} times".format(dice, counts[dice]))
这将为您提供:

(1, 1) occurred 22 times
(1, 2) occurred 53 times
(1, 3) occurred 47 times
(1, 4) occurred 55 times
(1, 5) occurred 55 times
(1, 6) occurred 50 times
(2, 2) occurred 27 times
(2, 3) occurred 64 times
(2, 4) occurred 58 times
...

这是两个无法区分的骰子的解决方案,这意味着投掷1和3的处理方式与投掷3和1的处理方式相同。在这种方法中,我们使用dict而不是list,因为对于两个或更多的用户!无法区分的骰子列表中会有像3,1这样的洞组合,这是永远不会出现的,因为我们将它们视为1,3

import random

counts = {}
for _ in range(1000):
    dice = tuple(sorted([random.randint(1, 6), random.randint(1, 6)]))
    counts[dice] = counts.get(dice, 0) + 1
骰子现在是两个骰子,经过排序,3,1被视为1,3,并从一个列表转换成一个元组,基本上是一个不可变的列表,所以我们可以使用它作为字典计数的键。然后我们只是增加骰子组合的数量

与列表不同的是,字典没有排序,但我们确实希望按照骰子显示的内容进行排序,因此我们按照keys=dice进行排序:

for dice in sorted(counts.keys()):
    print("{} occurred {} times".format(dice, counts[dice]))
这将为您提供:

(1, 1) occurred 22 times
(1, 2) occurred 53 times
(1, 3) occurred 47 times
(1, 4) occurred 55 times
(1, 5) occurred 55 times
(1, 6) occurred 50 times
(2, 2) occurred 27 times
(2, 3) occurred 64 times
(2, 4) occurred 58 times
...

不,我只需要这个程序来掷两个骰子,这样以后我就可以在掷两个三的地方进行编程,打印出两个三的骰子…次,所以我只需要弄清楚如何掷两个骰子,如果有意义的话?不,我只需要这个程序来掷两个骰子,这样以后我就可以在两个三的地方进行编程 是滚动的吗?它打印了两个3…滚动了几次,所以我需要弄清楚如何滚动两个骰子,如果这有意义的话?