Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
pprz_random.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Joost Meulenbeld
3  *
4  * This file is part of paparazzi
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
26 #include "pprz_random.h"
27 
28 #ifdef BOARD_CONFIG
29 #include "mcu_periph/sys_time.h"
30 #else
31 #include <time.h>
32 #endif
33 
34 
35 void init_random(void)
36 {
37 #ifdef BOARD_CONFIG
38  srand(get_sys_time_msec());
39 #else
40  srand(time(NULL));
41 #endif
42 
43 }
44 
45 double rand_uniform(void)
46 {
47  return (double) rand() / RAND_MAX;
48 }
49 
50 /*
51  * http://www.taygeta.com/random/gaussian.html
52  */
53 double rand_gaussian(void)
54 {
55  static int nb_call = 0;
56  static double x2;
57  static double w;
58  double x1;
59 
60  nb_call++;
61  if (nb_call % 2) {
62  do {
63  x1 = 2.0 * rand_uniform() - 1.0;
64  x2 = 2.0 * rand_uniform() - 1.0;
65  w = x1 * x1 + x2 * x2;
66  } while (w >= 1.0 || w == 0.0);
67 
68  w = sqrt((-2.0 * log(w)) / w);
69  return x1 * w;
70  } else {
71  return x2 * w;
72  }
73 }
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:98
void init_random(void)
Definition: pprz_random.c:35
double rand_gaussian(void)
Definition: pprz_random.c:53
double rand_uniform(void)
Definition: pprz_random.c:45
Architecture independent timing functions.