如何分配 Python 中的值?
How to distribute values in Python?
所以我正在尝试计算 CSGO 皮肤上漂浮的可能性。
浮点数是 0 和 1 之间的值,它们分为 五个部分 。
崭新出厂 (0 到 0.07)3%,略有磨损 (0.07 到 0.14)24%,久经沙场 (0.14 到 0.38) 33%,破旧不堪 (0.38 到 0.45) 24% 和战痕累累 (0.45 到 1.0) 16%.
如您所见,浮点值之间的分布并不均匀,而是加权。然而,在每个部分中,值然后 均匀分布 ,例如:
https://blog.csgofloat.com/content/images/2020/07/image-6.png
当你引入 float caps 时,它会变得棘手,这意味着 float 不再介于 0 和 1 之间,而是介于 0.14 和 0.65 之间。
该值的计算方式如下:
- 根据权重选择一个版块。
- 该部分范围内的浮点数是随机生成的。
- 最后的浮点数是根据这个公式计算出来的:
final_float = 浮动 * (max_float - min_float) + min_float
float 是随机生成的值,最大值和 min_float 上限和下限(在本例中为 0.14 和 0.65)。
我现在想计算分布有上限的皮肤在五个部分。
我该怎么做?
提前谢谢你。
使用numpy
库很简单:
import numpy as np
# input data
n_types = 5
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([0.0, 0.07, 0.14, 0.38, 0.45, 1.0])
# simulate distribution, by generating `n_samples` random floats
n_samples = 1000000
type_samples = np.random.choice(range(n_types), p=types_weights, size=n_samples, replace=True, )
float_ranges_begin = types_intervals[type_samples]
float_ranges_end = types_intervals[type_samples + 1]
float_samples = float_ranges_begin + np.random.rand(n_samples) * (float_ranges_end - float_ranges_begin)
# plot results
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
plt.hist(float_samples, bins=100, density=True, rwidth=0.8)
# to see types names instead
# plt.xticks(types_intervals, types + ['1.0'], rotation='vertical', fontsize=16)
plt.xlabel('Float', fontsize=16)
plt.ylabel('Probability density', fontsize=16);
编辑
如果你想找到 精确 分布,那么这也很容易,虽然你的“可扩展”要求对我来说并不完全清楚
n_types = 5
types = ['Factory New', 'Minimal Wear', 'Field-Tested', 'Well-Worn', 'Battle-Scarred']
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([-0.0001, 0.07, 0.14, 0.38, 0.45, 1.0])
# corerspond to top values on my plot, approximately [0.4 3.4 1.37 3.4 0.3]
types_probability_density = types_weights / (types_intervals[1:] - types_intervals[:-1])
def float_probability_density(x):
types = np.searchsorted(types_intervals, x) - 1
return types_probability_density[types]
sample_floats = np.linspace(0.0, 1.0, 100)
plt.figure(figsize=(16,8))
plt.bar(sample_floats, float_probability_density(sample_floats), width=0.005)
所以我正在尝试计算 CSGO 皮肤上漂浮的可能性。 浮点数是 0 和 1 之间的值,它们分为 五个部分 。
崭新出厂 (0 到 0.07)3%,略有磨损 (0.07 到 0.14)24%,久经沙场 (0.14 到 0.38) 33%,破旧不堪 (0.38 到 0.45) 24% 和战痕累累 (0.45 到 1.0) 16%.
如您所见,浮点值之间的分布并不均匀,而是加权。然而,在每个部分中,值然后 均匀分布 ,例如: https://blog.csgofloat.com/content/images/2020/07/image-6.png
当你引入 float caps 时,它会变得棘手,这意味着 float 不再介于 0 和 1 之间,而是介于 0.14 和 0.65 之间。
该值的计算方式如下:
- 根据权重选择一个版块。
- 该部分范围内的浮点数是随机生成的。
- 最后的浮点数是根据这个公式计算出来的: final_float = 浮动 * (max_float - min_float) + min_float float 是随机生成的值,最大值和 min_float 上限和下限(在本例中为 0.14 和 0.65)。
我现在想计算分布有上限的皮肤在五个部分。
我该怎么做? 提前谢谢你。
使用numpy
库很简单:
import numpy as np
# input data
n_types = 5
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([0.0, 0.07, 0.14, 0.38, 0.45, 1.0])
# simulate distribution, by generating `n_samples` random floats
n_samples = 1000000
type_samples = np.random.choice(range(n_types), p=types_weights, size=n_samples, replace=True, )
float_ranges_begin = types_intervals[type_samples]
float_ranges_end = types_intervals[type_samples + 1]
float_samples = float_ranges_begin + np.random.rand(n_samples) * (float_ranges_end - float_ranges_begin)
# plot results
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
plt.hist(float_samples, bins=100, density=True, rwidth=0.8)
# to see types names instead
# plt.xticks(types_intervals, types + ['1.0'], rotation='vertical', fontsize=16)
plt.xlabel('Float', fontsize=16)
plt.ylabel('Probability density', fontsize=16);
编辑
如果你想找到 精确 分布,那么这也很容易,虽然你的“可扩展”要求对我来说并不完全清楚
n_types = 5
types = ['Factory New', 'Minimal Wear', 'Field-Tested', 'Well-Worn', 'Battle-Scarred']
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([-0.0001, 0.07, 0.14, 0.38, 0.45, 1.0])
# corerspond to top values on my plot, approximately [0.4 3.4 1.37 3.4 0.3]
types_probability_density = types_weights / (types_intervals[1:] - types_intervals[:-1])
def float_probability_density(x):
types = np.searchsorted(types_intervals, x) - 1
return types_probability_density[types]
sample_floats = np.linspace(0.0, 1.0, 100)
plt.figure(figsize=(16,8))
plt.bar(sample_floats, float_probability_density(sample_floats), width=0.005)