Python 从每个 bin 的范围内抽取随机值
Python sampling random value from range of each bin
我目前正在尝试使用 CiLEO 作为参考来实施 NASA 解体模型。我对如何实现作者定义的特征长度感到非常困惑:
"For what concerns the implementation in CiELO, the characteristic length is divided into 100 bins equally spaced on a logarithmic scale between 1 mm and 10 cm: the number of fragments for each bin is computed and once each fragment is assigned to one bin, its characteristic length is defined using the built-in matlab function rand to extract a random value for Lc within the bin."
我的问题是最后一个组件,在每个 bin 中提取一个随机值来定义长度。我在网上找不到任何关于如何在给定 bin 边缘的每个 bin 中选择一个值的信息。
为了更容易演示,我目前将片段数函数定义为:
def number_of_fragments(length):
return 0.1*length**-1.71
# Generating 100 bins evenly spaced on a log scale between 1mm (0.001 m) and 10cm (0.1 m)
bins = np.geomspace(0.001, 0.1, 100)
# Finding the corresponding number of fragments for each bin
fragments = [number_of_fragments(b) for b in bins]
# Need to pick a random value that falls in the range of each bin
Lc = ???
如有任何帮助,我们将不胜感激!
像这样:
from random import uniform
bins = np.geomspace(0.001, 0.1, 100)
rand_floats = [uniform(bins[i], bins[i+1]) for i in range(len(bins)-1)]
通过索引遍历列表项并动态设置随机数生成范围。
编辑:请注意,这假设均匀分布。
请参阅文档:https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html
我目前正在尝试使用 CiLEO 作为参考来实施 NASA 解体模型。我对如何实现作者定义的特征长度感到非常困惑:
"For what concerns the implementation in CiELO, the characteristic length is divided into 100 bins equally spaced on a logarithmic scale between 1 mm and 10 cm: the number of fragments for each bin is computed and once each fragment is assigned to one bin, its characteristic length is defined using the built-in matlab function rand to extract a random value for Lc within the bin."
我的问题是最后一个组件,在每个 bin 中提取一个随机值来定义长度。我在网上找不到任何关于如何在给定 bin 边缘的每个 bin 中选择一个值的信息。
为了更容易演示,我目前将片段数函数定义为:
def number_of_fragments(length):
return 0.1*length**-1.71
# Generating 100 bins evenly spaced on a log scale between 1mm (0.001 m) and 10cm (0.1 m)
bins = np.geomspace(0.001, 0.1, 100)
# Finding the corresponding number of fragments for each bin
fragments = [number_of_fragments(b) for b in bins]
# Need to pick a random value that falls in the range of each bin
Lc = ???
如有任何帮助,我们将不胜感激!
像这样:
from random import uniform
bins = np.geomspace(0.001, 0.1, 100)
rand_floats = [uniform(bins[i], bins[i+1]) for i in range(len(bins)-1)]
通过索引遍历列表项并动态设置随机数生成范围。
编辑:请注意,这假设均匀分布。 请参阅文档:https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html