codec: avcodec: remove leftoff cc code
[vlc.git] / src / misc / rand.c
blob0c6ab651468bb9ed116f067b83118d659a3d80bd
1 /*****************************************************************************
2 * rand.c : non-predictible random bytes generator
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
5 * $Id$
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.
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.
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <vlc_common.h>
27 #include <vlc_rand.h>
29 static struct
31 bool init;
32 unsigned short subi[3];
33 vlc_mutex_t lock;
34 } rand48 = { false, { 0, 0, 0, }, VLC_STATIC_MUTEX, };
36 static void init_rand48 (void)
38 if (!rand48.init)
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
49 /**
50 * PRNG uniformly distributed between 0.0 and 1.0 with 48-bits precision.
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.
56 * @return a double value within [0.0, 1.0] inclusive
58 double vlc_drand48 (void)
60 double ret;
62 vlc_mutex_lock (&rand48.lock);
63 init_rand48 ();
64 ret = erand48 (rand48.subi);
65 vlc_mutex_unlock (&rand48.lock);
66 return ret;
69 /**
70 * PRNG uniformly distributed between 0 and 2^32 - 1.
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.
76 * @return an integral value within [0.0, 2^32-1] inclusive
78 long vlc_lrand48 (void)
80 long ret;
82 vlc_mutex_lock (&rand48.lock);
83 init_rand48 ();
84 ret = nrand48 (rand48.subi);
85 vlc_mutex_unlock (&rand48.lock);
86 return ret;
89 /**
90 * PRNG uniformly distributed between -2^32 and 2^32 - 1.
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.
96 * @return an integral value within [-2^32, 2^32-1] inclusive
98 long vlc_mrand48 (void)
100 long ret;
102 vlc_mutex_lock (&rand48.lock);
103 init_rand48 ();
104 ret = jrand48 (rand48.subi);
105 vlc_mutex_unlock (&rand48.lock);
106 return ret;