如何使用 Python 中的离散均匀分布将 0-1 范围内的数字转换为 1-52 范围内的数字?
How can I transform numbers in a range of 0-1 to a 1-52 range using Discrete Uniform Distribution in Python?
我正在寻找 Python 中的方法或任何其他方法来解决这个问题,因为我知道有一种方法,但我找不到它,在我的上下文中做的是使用蒙特卡洛方法计算一对,两对等出现在扑克游戏中的概率,目前我正在使用 random.sample
函数来做到这一点,但我想用我自己的方法来生成return 介于 0-1 之间的随机数,我想使用离散均匀分布来确保每个数字都有相同的出现概率。
我很感激你能提供的任何帮助。
如果数字是0到1之间的浮点数(范围[0, 1)),你可以这样做:
int(random() * 52)
获取 0 到 51 之间的随机 int
(范围 [0, 51])。
或者:
int(random() * 52) + 1
获取 1 到 52 之间的随机 int
(范围 [1, 52])。
这道数学题比python还多。这是解决方案:
代码:
import random
arr_0_1 = [random.random() for i in range(10)]
print(arr_0_1)
d = 1/51
arr_1_52 = [n/d + 1 for n in arr_0_1]
print(arr_1_52)
输出:
[0.49864143060719257, 0.9124661869543931, 0.14064674065024652, 0.6162308610305717, 0.698038549502753, 0.18002207940954196, 0.7337352732415695, 0.34955525833304546, 0.6380173638897209, 0.14733996159513485]
[26.430712960966822, 47.535775534674045, 8.172983773162573, 32.42777391255916, 36.5999660246404, 10.18112604988664, 38.42049893532005, 18.82731817498532, 33.53888555837577, 8.514338041351877]
Solution
这个解决方案也是有效的,它给了我相同的结果。
我正在寻找 Python 中的方法或任何其他方法来解决这个问题,因为我知道有一种方法,但我找不到它,在我的上下文中做的是使用蒙特卡洛方法计算一对,两对等出现在扑克游戏中的概率,目前我正在使用 random.sample
函数来做到这一点,但我想用我自己的方法来生成return 介于 0-1 之间的随机数,我想使用离散均匀分布来确保每个数字都有相同的出现概率。
我很感激你能提供的任何帮助。
如果数字是0到1之间的浮点数(范围[0, 1)),你可以这样做:
int(random() * 52)
获取 0 到 51 之间的随机 int
(范围 [0, 51])。
或者:
int(random() * 52) + 1
获取 1 到 52 之间的随机 int
(范围 [1, 52])。
这道数学题比python还多。这是解决方案:
代码:
import random
arr_0_1 = [random.random() for i in range(10)]
print(arr_0_1)
d = 1/51
arr_1_52 = [n/d + 1 for n in arr_0_1]
print(arr_1_52)
输出:
[0.49864143060719257, 0.9124661869543931, 0.14064674065024652, 0.6162308610305717, 0.698038549502753, 0.18002207940954196, 0.7337352732415695, 0.34955525833304546, 0.6380173638897209, 0.14733996159513485]
[26.430712960966822, 47.535775534674045, 8.172983773162573, 32.42777391255916, 36.5999660246404, 10.18112604988664, 38.42049893532005, 18.82731817498532, 33.53888555837577, 8.514338041351877]
Solution
这个解决方案也是有效的,它给了我相同的结果。