# Best Way to Randomly Adjust a Vector

This topic is 2911 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Let's say I have a rapid-fire weapon and to simulate recoil I want to fire the bullets spreading slightly out randomly by a given amount.
I have seen picking sphere points at random but this is not so useful.
http://mathworld.wolfram.com/SpherePointPicking.html

These routines do not accept an input vector.
I have a vector that my player or bot is facing and I want to adjust within tolerance from there.

I am looking for a routine that accepts an input 3D vector and a maximum range and outputs a vector representing the input vector adjusted by a random amount within the tolerance.

Thank you,
Yogurt Emperor

##### Share on other sites
No code handy, but maybe it's worth to take a look at some of several schemes to sample some BRDFs, like e.g. phong.

##### Share on other sites
Thank you for the direction, but I don’t know if this is for what I was searching.
I have seen code for this somewhere and it is quite short and simple.

Yogurt Emperor

##### Share on other sites
A simple (but not necessarily best) way would be: Compute a 2D vector from a random angle in [0,360)
a := random( 0, 360 );
and a random spreading range (err, correct wording?) in [0,D)
d := random( 0, D );
so that
v := [ cos(a) sin(a) 0 ]t * d

Add this to the local shooting direction vector, so that
v' := [ 0 0 1 ]t * R + v
where R denotes the distance at which the spreading should have the extent D.

Normalize this vector
v" := v' / ||v'||
and use this as new shooting direction. It is in local space, of course, and computed with z to be the standard shooting direction. So you have to transform it into the space where needed as usual.

##### Share on other sites
If you want a uniform distribution on a spherical cap centered around a given vector, you can do this:
* Pick a random number z between cos(alpha) and 1. The parameter alpha controls the size of the cap.
* Compute r = sqrt(1-z*z).
* Pick a random angle theta.
* Build the vector (r * cos(theta), r * sin(theta), z)
* Apply a rotation to this vector that would turn (0, 0, 1) into your original vector.

You can get other distributions by picking z differently in the procedure above.

If you are not particularly concerned with the distribution you get, you can use a much simpler procedure: add a little random vector to your original vector (it doesn't matter much from what distribution; I would start with a normal) and then normalize the result.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633740
• Total Posts
3013621
×