Post · 1 min read· pp. 328–329

Random Attitudes

ahrs.common.quaternion.random_attitudes(n: int = 1, representation: str = 'quaternion') → ndarray : Generate random attitudes

code
To generate a random quaternion a mapping in SO(3) is first created and
then transformed as explained originally by [[Sho92](/u/fab8b0a9fec670cf#id69)] and
summarized in [[Kuf04](/u/fab8b0a9fec670cf#id37)].

First, three uniform random values are sampled from the interval [0, 1]:

u1,u2,u3∼U(0,1)u_1, u_2, u_3 \sim U(0, 1)
Then, two random angles are generated from these values:
s1=1−u1s2=u1t1=2πu2t2=2πu3\begin{split}\begin{array}{rl} s_1 &= \sqrt{1-u_1} \\ s_2 &= \sqrt{u_1} \\ t_1 &= 2\pi u_2 \\ t_2 &= 2\pi u_3 \end{array}\end{split}
Finally, the quaternion is built as:
q=[s2cos⁡t2s1sin⁡t1s1cos⁡t1s2sin⁡t2]\begin{split}\mathbf{q} = \begin{bmatrix} s_2\cos t_2 \\ s_1\sin t_1 \\ s_1\cos t_1 \\ s_2\sin t_2 \end{bmatrix}\end{split}
Parameters: : - **n** (*int, default: 1*) – Number of random attitudes to generate. Default is 1. - **representation** (str, default: `'quaternion'`) – Attitude representation. Options are `'quaternion'` or `'rotmat'`. Returns: : **Q** – Array of n random quaternions. Return type: : numpy.ndarray Examples ```python

random_attitudes() array([ 0.17607048, -0.42133834, -0.42445916, -0.78186163])

random_attitudes(1) array([ 0.50391486, 0.15642284, -0.04152787, -0.84845574])

random_attitudes(3) array([[-0.78506768, -0.21469241, 0.57654945, -0.07187942], [ 0.35614954, 0.78811481, 0.45912792, -0.20306182], [-0.2474619 , 0.21632585, 0.50492025, 0.79813613]])

random_attitudes(3, 'rotmat') array([[[-0.43297822, 0.69987831, -0.56806708], [-0.07346304, -0.65550383, -0.75161021], [-0.89840583, -0.28369892, 0.33523407]],

code
   [[ 0.1082323 , -0.55111441,  0.82738061],
    [-0.55001356,  0.66008952,  0.51163162],
    [-0.82811283, -0.51044562, -0.23167739]],

   [[ 0.19915247,  0.86104088, -0.46791762],
    [-0.13040079, -0.44995169, -0.88348124],
    [-0.97125379,  0.2369643 ,  0.02267142]]])
code