On Tue, May 17, 2022 at 5:20 AM <saro...@gmail.com> wrote:

> ### Proposed new feature or change:
>
> Description
> ========
>
> Often you want to randomize a "direction" that doesn't have "size". This
> can be useful when:
> * You want to randomize the direction of an NPC walk developing in a 2D or
> 3D games, where its speed is a predefined constant.
> * You're developing particles simulation with all particles moving in
> random directions
> * etc.
>
> This random direction should be an `n`th dimensional unit vector which is
> randomize **uniformly** from the unit sphere. That means that sections of
> the unit sphere with equal areas should have the same chance of getting a
> vector from.
>
> Implementation
> ===========
>
> If one wants to randomize `n`th dimensional direction uniformly, one must
> do the following:
> 1. Generate `n` components of the vector using the standard normal
> distribution
> 2. Find the norm the generated vector
> 3. Divide the vector by its norm
>
> In simple Python code:
> ```python
> import numpy as np
>
>
> def sphere_uniform(n: int):
>     vec = np.random.normal(size=n)
>     norm = np.sqrt(np.sum(vec ** 2))
>     return vec / norm
> ```
>
> This implementation suggestion is based on [this blog post](
> https://towardsdatascience.com/the-best-way-to-pick-a-unit-vector-7bd0cc54f9b).
> I'm not sure if there's a faster way of implementing it using C++ and
> Cython, but I'm confident that someone here will know.
>

I would probably use np.linalg.norm() to calculate the norm, but mostly for
readability.


> Why Implement It in Numpy?
> ===================
>
> I believe that random unit vectors are common enough to be a part of
> Numpy. Scientists, gamers and engineers use random directions all the time,
> and I believe there is no reason why Numpy won't provide this ability.
>

As a general policy, we do not plan on adding more methods to Generator
(c.f. discussion of a previous suggested addition[1]). This would be a good
multivariate distribution to add to scipy.stats, though.

[1] https://www.mail-archive.com/numpy-discussion@python.org/msg04724.html

-- 
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to