设计有偏掷骰子函数的Python程序
在本文中,任务是编写一个Python程序来生成一个无偏的掷骰子并给出相应的输出。为此,我们可以使用 Python 随机库中的各种方法。
方法:
- 使用 random.choice() 方法
- 使用 random.randrange() 方法
- 使用 random.randint() 方法
- 使用 random.random() 方法
- 使用 random.choices() 方法
方法 1:使用random.choice()
Choice() 是Python编程语言中的一个内置函数,它从列表、元组或字符串中返回一个随机项。此方法专为从容器中获取随机数的特定目的而设计,因此是实现从列表中获取随机数这一任务的最常用方法。
Syntax: random.choice(sequence)
Parameters:
- Sequence is a mandatory parameter that can be a list, tuple, or string.
Returns: The choice() returns a random item.
现在,我们知道骰子有 1-6 范围内的数字,我们想要一个有偏差的结果作为输出,所以我们将给出 [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6 , 6] 作为函数和函数中的序列,由于数字的频率不同,函数将给出一个有偏差的掷骰子作为输出。下面是基于上述解释的实现:
Python3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# Printing the random result
print(random.choice(sequence))
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
Python3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.randrange() to
# get a random index number
rand_idx = random.randrange(len(sequence))
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
Python3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.randint() to
# get a random index number
rand_idx = random.randint(0, len(sequence)-1)
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
Python3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.random() to
# get a random number
rand_idx = int(random.random() * len(sequence))
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
Python3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 6]
# using random.choices() to
# get a random choice
rand_choice = random.choices(sequence, weights = [1, 2, 3, 4, 5, 6], k = 1)
# Printing the random result
print(*rand_choice)
# Driver Code
# Calling the function
dice_throw()1
6
3
# Calling the function
dice_throw()
# Calling the function
dice_throw()
输出:
6
3
6
方法 2:使用 random.randrange()
该方法用于生成一个范围内的随机数,对于列表,我们可以指定范围为0到其长度,并获取索引,然后获取对应的值。这也提供了在某个倍数的索引处获取偶数放置元素或元素的选项。
Syntax : random.randrange(start(opt), stop,step(opt))
Parameters :
- start(opt) : Number consideration for generation starts from this, default value is 0. This parameter is optional.
- stop : Numbers less than this are generated. This parameter is mandatory.
- step(opt) : Step point of range, this won’t be included. This is optional. Default value is 1.
Return Value : This function generated the numbers in the sequence start-stop skipping step.
现在,我们知道骰子有 1-6 范围内的数字,我们想要一个有偏差的结果作为输出,所以我们将给出 [1, 1, 1, 2, 3, 3, 4, 5, 6, 6 , 6, 6] 作为函数中的序列,然后使用 randrange 找到一个随机索引,因此函数将给出一个有偏差的掷骰子作为输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.randrange() to
# get a random index number
rand_idx = random.randrange(len(sequence))
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
输出:
6
3
3
方法 3:使用 random.randint()
还有一种生成随机数的方法,也可以用来生成一个范围内的任意一个数,然后利用那个数索引,我们就可以找到对应索引处的值,就像上面提到的技术一样。但它的不同之处在于它需要 2 个强制参数范围。
Syntax : randint(start, end)
Parameters :
- (start, end) : Both of them must be integer type values.
Returns : A random integer in range [start, end] including the end points.
现在,我们知道骰子有 1-6 范围内的数字,我们想要一个有偏差的结果作为输出,所以我们将给出 [1, 1, 1, 2, 3, 3, 4, 5, 6, 6 , 6, 6] 作为函数中的序列,然后使用 randint 找到一个随机索引,因此函数将给出一个有偏差的掷骰子作为输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.randint() to
# get a random index number
rand_idx = random.randint(0, len(sequence)-1)
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
输出:
5
2
4
方法 4:使用 random.random()
此方法生成0到1范围内的浮点数。我们也可以使用此函数通过将结果相乘然后类型转换为int以获得整数索引然后对应的列表值来获取列表的索引值。然后我们将使用该索引值从列表中打印一个随机数。
Syntax : random.random()
Parameters : This method does not accept any parameter.
Returns : This method returns a random floating number between 0 and 1.
现在,我们知道骰子有 1-6 范围内的数字,我们想要一个有偏差的结果作为输出,所以我们将给出 [1, 1, 1, 2, 3, 3, 4, 5, 6, 6 , 6, 6] 作为函数中的序列,然后使用 random 找到一个随机索引,因此函数将给出一个有偏差的掷骰子作为输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6]
# using random.random() to
# get a random number
rand_idx = int(random.random() * len(sequence))
# Printing the random result
print(sequence[rand_idx])
# Driver Code
# Calling the function
dice_throw()
# Calling the function
dice_throw()
# Calling the function
dice_throw()
输出:
6
2
6
方法 5:使用 random.choices()
choice() 方法从列表中返回多个随机元素,并进行替换。您可以使用 weights 参数或 cum_weights 参数衡量每个结果的可能性。元素可以是字符串、范围、列表、元组或任何其他类型的序列。
Syntax : random.choices(sequence, weights=None, cum_weights=None, k=1)
Parameters :
- sequence: It is a mandatory parameter that can be a list, tuple, or string.
- weights: It is an optional parameter which is used to weigh the possibility for each value.
- cum_weights: It is an optional parameter which is used to weigh the possibility for each value but in this the possibility is accumulated
- k: It is an optional parameter that is used to define the length of the returned list.
现在,我们知道骰子有 1-6 范围内的数字,所以我们将 [1, 2, 3, 4, 5, 6] 作为一个序列,然后我们将设置列表中每个元素的权重.然后我们将打印输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design a biased dice throw
# function
# Importing random library
import random
# Function to give a biased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 6]
# using random.choices() to
# get a random choice
rand_choice = random.choices(sequence, weights = [1, 2, 3, 4, 5, 6], k = 1)
# Printing the random result
print(*rand_choice)
# Driver Code
# Calling the function
dice_throw()1
6
3
# Calling the function
dice_throw()
# Calling the function
dice_throw()
输出:
1
6
3