generate_mie_costheta

miepython.monte_carlo.generate_mie_costheta(mu_cdf, rng=None)[source]

Generate a new scattering angle using a cdf.

A uniformly spaced cumulative distribution function (CDF) is needed. New random angles are generated by selecting a random interval mu[i] to mu[i+1] and choosing an angle uniformly distributed over the interval.

Parameters:
  • mu_cdf – a cumulative distribution function

  • rng – optional source of randomness, anything with a random() method such as numpy.random.default_rng(). Omitting it uses the process wide numpy.random, whose stream a caller cannot control without reseeding the whole program, so pass a generator when reproducibility matters.

Returns:

The cosine of the scattering angle

Examples

Draw a reproducible angle without disturbing the global stream:

>>> import numpy as np
>>> import miepython.monte_carlo as mc
>>> mu, cdf = mc.mu_with_uniform_cdf(1.33, 1.7, 50)
>>> rng = np.random.default_rng(42)
>>> first = mc.generate_mie_costheta(mu, rng=rng)
>>> bool(-1.0 <= first <= 1.0)
True
>>> bool(first == mc.generate_mie_costheta(mu, rng=np.random.default_rng(42)))
True