设计无偏掷骰子函数的Python程序
任务是编写一个Python程序来生成一个无偏的掷骰子并给出相应的输出。为此,我们可以使用Python random模块中存在的各种方法。
方法:
- 使用 random.choice() 方法
- 使用 random.randrange() 方法
- 使用 random.randint() 方法
- 使用 random.random() 方法
方法 1:使用 random.choice()
choice()是random模块中的一个方法,它从列表、元组或字符串中返回一个随机项。此方法专为从容器中获取随机数的特定目的而设计,因此是实现从列表中获取随机数这一任务的最常用方法。
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, 2, 3, 4, 5, 6] 作为函数和函数中的序列将给出一个无偏的掷骰子作为输出.下面是基于上述解释的实现:
Python3
# Python program to design an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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 an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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 an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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 an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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()
输出:
3
1
4
方法 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, 2, 3, 4, 5, 6] 作为函数中的序列,然后使用randrange()找到一个随机index 和函数将给出一个无偏的掷骰子作为输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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()
输出:
3
1
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, 2, 3, 4, 5, 6] 作为函数中的序列,然后使用randint()找到一个随机index 和函数将给出一个无偏的掷骰子作为输出。下面是基于上述解释的实现:
蟒蛇3
# Python program to design an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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()
输出:
1
5
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.
蟒蛇3
# Python program to design an unbiased dice throw
# function
# Importing random library
import random
# Function to give an unbiased dice throw
def dice_throw():
# Declaring the sequence
sequence = [1, 2, 3, 4, 5, 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()
输出:
2
6
1