]> git.sesse.net Git - vlc/blob - src/misc/rand.c
decoder: inline DecoderSignalWait()
[vlc] / src / misc / rand.c
1 /*****************************************************************************
2  * rand.c : non-predictible random bytes generator
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_rand.h>
28
29 static struct
30 {
31     bool           init;
32     unsigned short subi[3];
33     vlc_mutex_t    lock;
34 } rand48 = { false, { 0, 0, 0, }, VLC_STATIC_MUTEX, };
35
36 static void init_rand48 (void)
37 {
38     if (!rand48.init)
39     {
40         vlc_rand_bytes (rand48.subi, sizeof (rand48.subi));
41         rand48.init = true;
42 #if 0 // short would be more than 16-bits ?
43         for (unsigned i = 0; i < 3; i++)
44             subi[i] &= 0xffff;
45 #endif
46     }
47 }
48
49 /**
50  * PRNG uniformly distributed between 0.0 and 1.0 with 48-bits precision.
51  *
52  * @note Contrary to POSIX drand48(), this function is thread-safe.
53  * @warning Series generated by this function are not reproducible.
54  * Use erand48() if you need reproducible series.
55  *
56  * @return a double value within [0.0, 1.0] inclusive
57  */
58 double vlc_drand48 (void)
59 {
60     double ret;
61
62     vlc_mutex_lock (&rand48.lock);
63     init_rand48 ();
64     ret = erand48 (rand48.subi);
65     vlc_mutex_unlock (&rand48.lock);
66     return ret;
67 }
68
69 /**
70  * PRNG uniformly distributed between 0 and 2^32 - 1.
71  *
72  * @note Contrary to POSIX lrand48(), this function is thread-safe.
73  * @warning Series generated by this function are not reproducible.
74  * Use nrand48() if you need reproducible series.
75  *
76  * @return an integral value within [0.0, 2^32-1] inclusive
77  */
78 long vlc_lrand48 (void)
79 {
80     long ret;
81
82     vlc_mutex_lock (&rand48.lock);
83     init_rand48 ();
84     ret = nrand48 (rand48.subi);
85     vlc_mutex_unlock (&rand48.lock);
86     return ret;
87 }
88
89 /**
90  * PRNG uniformly distributed between -2^32 and 2^32 - 1.
91  *
92  * @note Contrary to POSIX mrand48(), this function is thread-safe.
93  * @warning Series generated by this function are not reproducible.
94  * Use jrand48() if you need reproducible series.
95  *
96  * @return an integral value within [-2^32, 2^32-1] inclusive
97  */
98 long vlc_mrand48 (void)
99 {
100     long ret;
101
102     vlc_mutex_lock (&rand48.lock);
103     init_rand48 ();
104     ret = jrand48 (rand48.subi);
105     vlc_mutex_unlock (&rand48.lock);
106     return ret;
107 }