request Vector.randomUnitVector()
See original GitHub issueHey, 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:
- Created 3 years ago
- Comments:18 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
orexamples/jsm/math/Random.js
can still be added then.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.