Reece Humphreys
Reece Humphreys

Reputation: 147

Python sampling random value from range of each bin

I am currently trying to implement the NASA breakup model using CiLEO as a reference. I am very confused as to how I would go about implementing the characteristic length which the author defines as follows:

"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."

My issue is with the last component, extracting a random value within each bin to define the length. I cannot find anything online on how to pick a value that falls within each bin given bin edges.

To make this easier to demonstrate I have currently defined the number of fragments function as:

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 = ???

Any help on this would be greatly appreciated!

Upvotes: 1

Views: 1085

Answers (1)

rwking
rwking

Reputation: 1032

Like this:

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)]

Iterate through list items by index and dynamically set the range for random number generation.

Edit: Note this assumes uniform distribution. See docs: https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html

Upvotes: 1

Related Questions