<p>The simplest way to do this would be to generate the Euler angles randomly, then use them to create the quaternion. Most existing quaternion implementations in game engines and 3D math libraries will have a <code>FromEuler ( Vector3 )</code> function. </p>

<p>Since you seem to be rolling your own quaternions rather than using a library, here's the <a href="https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Euler_Angles_to_Quaternion_Conversion" rel="nofollow noreferrer">conversion formula</a>.</p>

<p>As a small optimization, since quaternions use half-angles, you could generate random values in the range ±15 degrees (pi/12) and feed these to the sin and cos functions directly.</p>

CC BY-SA 4.0
2018-10-28T16:58:05.527Z
Thanks Russ. Could you please take a look at my new, relevant question [here](https://computergraphics.stackexchange.com/questions/8195/how-to-convert-euler-angles-to-quaternions-and-get-the-same-euler-angles-back) and see if you can provide an answer for this one as well?
Amir
CC BY-SA 4.0
2018-10-27T22:31:41.873Z
2018-10-27T22:31:41.873Z
russ
<p>I am pretty new to Quaternions so please bear with me. I want to draw random Quaternion samples so that their Euler angle equivalent would range within <code>[-30, +30]</code> degrees on each axis. Currently, I know how to sample Quaternions from the full range (<code>[-180, +180]</code>) using the code below but I don't know how to modify the code so that I can get samples within the range <code>[-30, +30]</code>. Can anyone help me with that? </p>

<p>I'm not sure if this is helpful to answer this question but here's a piece of information: I eventually want to convert the sampled Quaternion to Euler angles an apply the Euler rotation in some 3D shapes. The order of rotation in the software I'm using to do this is <code>XYZ</code> meaning that it first rotates the 3D shape along the <code>X</code> axis, then <code>Y</code> axis and then <code>Z</code> axis.</p>

<pre><code>import numpy as np
def sample_Quaternion():
    r = np.random.uniform(0, 1 - 0.001, 3)
    while np.linalg.norm(r) &gt; 1:
        r = np.random.uniform(0, 1 - 0.001, 3) # Just to keep the L2 norm within [0, 1.0)
    w = [np.sqrt(1 - (r[0]*r[0] + r[1]*r[1] + r[2]*r[2]))]
    r = np.concatenate(r, w) # the output of this would represent (x, y, z, w)
    return r
</code></pre>

CC BY-SA 4.0
2018-10-28T01:48:53.817Z
"*I eventually want to convert the sampled Quaternion to Euler angles an apply the Euler rotation in some 3D shapes.*" Please stop wanting to do that. Euler angles are bad, and you've taken a big step by moving away from them. Keep your orientations as quaternions; don't convert to/from Euler angles.
Nicol Bolas
CC BY-SA 4.0
2018-10-28T16:57:44.247Z
@NicolBolas I really want to avoid using Euler angles but unfortunately I have to use Euler angles for a part of my current project. Could you also take a look at my new, relevant question [here](https://computergraphics.stackexchange.com/questions/8195/how-to-convert-euler-angles-to-quaternions-and-get-the-same-euler-angles-back) and see if you can provide an answer for this one?
Amir
CC BY-SA 4.0
2018-11-02T02:51:58.670Z
@Amir Why do not you just use matrices to rotate?
information
<3d><mathematics><geometry>
How to randomly draw Quaternions within a specific range of Euler angles for rotation?
Amir
# How to randomly draw Quaternions within a specific range of Euler angles for rotation?

(Asked by: username_0 on 2018-10-27)

I am pretty new to Quaternions so please bear with me. I want to draw random Quaternion samples so that their Euler angle equivalent would range within `[-30, +30]` degrees on each axis. Currently, I know how to sample Quaternions from the full range (`[-180, +180]`) using the code below but I don't know how to modify the code so that I can get samples within the range `[-30, +30]`. Can anyone help me with that?

I'm not sure if this is helpful to answer this question but here's a piece of username_2: I eventually want to convert the sampled Quaternion to Euler angles an apply the Euler rotation in some 3D shapes. The order of rotation in the software I'm using to do this is `XYZ` meaning that it first rotates the 3D shape along the `X` axis, then `Y` axis and then `Z` axis.

```
import numpy as np
def sample_Quaternion():
    r = np.random.uniform(0, 1 - 0.001, 3)
    while np.linalg.norm(r) > 1:
        r = np.random.uniform(0, 1 - 0.001, 3) # Just to keep the L2 norm within [0, 1.0)
    w = [np.sqrt(1 - (r[0]*r[0] + r[1]*r[1] + r[2]*r[2]))]
    r = np.concatenate(r, w) # the output of this would represent (x, y, z, w)
    return r

```

**Tags:** 3d, mathematics, geometry

> **Comments:**
> **username_1**: "*I eventually want to convert the sampled Quaternion to Euler angles an apply the Euler rotation in some 3D shapes.*" Please stop wanting to do that. Euler angles are bad, and you've taken a big step by moving away from them. Keep your orientations as quaternions; don't convert to/from Euler angles.  
> **username_0**: @username_1 I really want to avoid using Euler angles but unfortunately I have to use Euler angles for a part of my current project. Could you also take a look at my new, relevant question [here](https://computergraphics.stackexchange.com/questions/8195/how-to-convert-euler-angles-to-quaternions-and-get-the-same-euler-angles-back) and see if you can provide an answer for this one?  
> **username_2**: @username_0 Why do not you just use matrices to rotate?  

---

## 1 answers

### Answer by username_3 on 2018-10-27. Score: 2 | Accepted answer

The simplest way to do this would be to generate the Euler angles randomly, then use them to create the quaternion. Most existing quaternion implementations in game engines and 3D math libraries will have a `FromEuler ( Vector3 )` function.

Since you seem to be rolling your own quaternions rather than using a library, here's the [conversion formula](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Euler_Angles_to_Quaternion_Conversion).

As a small optimization, since quaternions use half-angles, you could generate random values in the range ±15 degrees (pi/12) and feed these to the sin and cos functions directly.

> **Comments:**
> **username_0**: Thanks username_3. Could you please take a look at my new, relevant question [here](https://computergraphics.stackexchange.com/questions/8195/how-to-convert-euler-angles-to-quaternions-and-get-the-same-euler-angles-back) and see if you can provide an answer for this one as well?  

---

