1 /*****************************************************************************
2 * rand.c : non-predictible random bytes generator
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
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 *****************************************************************************/
26 #include <vlc_common.h>
32 unsigned short subi
[3];
34 } rand48
= { false, { 0, 0, 0, }, VLC_STATIC_MUTEX
, };
36 static void init_rand48 (void)
40 vlc_rand_bytes (rand48
.subi
, sizeof (rand48
.subi
));
42 #if 0 // short would be more than 16-bits ?
43 for (unsigned i
= 0; i
< 3; i
++)
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)
62 vlc_mutex_lock (&rand48
.lock
);
64 ret
= erand48 (rand48
.subi
);
65 vlc_mutex_unlock (&rand48
.lock
);
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)
82 vlc_mutex_lock (&rand48
.lock
);
84 ret
= nrand48 (rand48
.subi
);
85 vlc_mutex_unlock (&rand48
.lock
);
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)
102 vlc_mutex_lock (&rand48
.lock
);
104 ret
= jrand48 (rand48
.subi
);
105 vlc_mutex_unlock (&rand48
.lock
);