question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Support Random Unit Vector

See original GitHub issue

Is your feature request related to a problem? Please describe.

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 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 nth dimensional unit vector which is randomized 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 nth 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:

import numpy as np
from scipy import stats


def sphere_uniform(n: int):
    vec = stats.norm.rvs(size=n)
    size = np.linalg.norm(vec)
    return vec / size

This implementation suggestion is based on this blog post. 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.

Why Implement It in Scipy?

Originally, I suggested implementing it on Numpy (numpy/numpy#21523), but they stated that they are not interested in expending the numpy.random library and suggested adding this new feature as part of scipy.stats.

I believe that random unit vectors are common enough to be a part of Numpy or Scipy, which are two of the most popular python libraries (if not the most popular ones). Scientists, gamers and engineers use random directions all the time, and I believe there is no reason why this ability shouldn’t be provided out-of-the-box.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
mdhabercommented, Jun 3, 2022

The former, I think. It would fit well as a subclass of multi_rv_generic in _multivariate.py.

2reactions
rkerncommented, May 18, 2022

Uniformly-random vectors on the unit hypersphere is an important multivariate distribution. It’s not hard to implement oneself, if one knows the necessary trick (and yes, the normal distribution is necessary). If one doesn’t know the appropriate trick, ad hoc implementations tend to fail in ways that are hard to diagnose in higher dimensions than 2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ENH: Support Random Unit Vector #21523 - numpy ... - GitHub
You want to randomize the direction of an NPC walk developing in a 2D or 3D games, where its speed is a predefined...
Read more >
How to Plot a unit vector in matlab - Stack Overflow
I tried creating 2 random points and 2 random unit vectors and using the function quiver to create the graph,
Read more >
Global sensitivity analysis using support vector regression
In this paper, a new kernel function derived from orthogonal polynomials is proposed for support vector regression (SVR). Based on this new kernel...
Read more >
How to normalize vectors to unit norm in Python - kawahara.ca
One way to normalize the vector is to apply some normalization to scale the vector to have a length of 1 i.e., a...
Read more >
RandomUnitVector | Wolfram Function Repository
Wolfram Language function: Generate random vectors in any dimension of unit length. Complete documentation and usage examples. Download an example notebook ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found