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.

request Vector.randomUnitVector()

See original GitHub issue

Hey, I’ve seen this in a handful of other libraries and always wondered why it wasn’t included. If you don’t use random vectors, then what are you doing with you life? I use it so often I add it in almost all my projects, it looks like this;

THREE.Vector3.prototype.randomUnitVector = function(){
    this.x = Math.random() * 2 - 1;
    this.y = Math.random() * 2 - 1;
    this.z = Math.random() * 2 - 1;
    this.normalize();
    return this;
}

You would call it like this; let v = new THREE.Vector3().randomUnitVector();

The name is quite long, maybe in relations to other vector functions names like .randomize() (like normalize) or just .random() would be more logical? You could also give it an argument to set the length, but I find it more logical to call .setLength() afterwards because it wouldn’t be a unit vector anymore and it’s easier to read. I’m curious of what you think of it.

Kind regards, Ethan.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
Mugen87commented, Mar 30, 2020

I still consider this whole topic application-specific.

You are right. Unless @mrdoob has a different opinion, I think I would prefer to wait if there are more similar feature requests in the future. Something like examples/jsm/math/Sampling.js or examples/jsm/math/Random.js can still be added then.

2reactions
greggmancommented, Apr 2, 2020

Just to add to the choir, for random colors I almost always prefer random hsl with a random hue but not random saturation or level (or some range but not 100% random). Like I don’t want something too light or too dark. In other words I agree with those claiming this is application specific.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Best Way to Pick a Unit Vector | by Don Cross
The random unit vector generator is very simple. Just call randomvector passing in the number of dimensions as n and you will get...
Read more >
Small way to get random vectors - Shadertoy
//with a random vector V in the range [-1, 1], you can get a relatively. 5. //uniformly distributed unit vector by doing normalize(tan(V))....
Read more >
random unit vector in multi-dimensional space - Stack Overflow
One simple trick is to select each dimension from a gaussian distribution, then normalize: from random import gauss def make_rand_vector(dims): vec ...
Read more >
Generating random unit vectors in n-dimensional space
It's a way to find two normally distributed coordinates, given a rand() function that returns values in the interval [0,1). I haven't confirmed...
Read more >
10.8 Unit vector | Stan Reference Manual
To generate a unit vector, Stan generates points at random in Rn R n with independent unit normal distributions, which are then standardized...
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