2 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * gstfastrandom.h: Fast, bad PNRG
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #ifndef __GST_FAST_RANDOM__
25 #define __GST_FAST_RANDOM__
27 /* transform [0..2^32] -> [0..1] */
28 #define GST_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
30 /* This is the base function, implementing a linear congruential generator
31 * and returning a pseudo random number between 0 and 2^32 - 1.
34 gst_fast_random_uint32 (void)
36 static guint32 state = 0xdeadbeef;
38 return (state = state * 1103515245 + 12345);
42 gst_fast_random_uint32_range (gint32 start, gint32 end)
44 guint64 tmp = gst_fast_random_uint32 ();
46 tmp = (tmp * (end - start)) / G_MAXUINT32 + start;
52 gst_fast_random_int32 (void)
54 return (gint32) gst_fast_random_uint32 ();
58 gst_fast_random_int32_range (gint32 start, gint32 end)
60 gint64 tmp = gst_fast_random_uint32 ();
62 tmp = (tmp * (end - start)) / G_MAXUINT32 + start;
68 gst_fast_random_double (void)
72 ret = gst_fast_random_uint32 () * GST_RAND_DOUBLE_TRANSFORM;
73 ret = (ret + gst_fast_random_uint32 ()) * GST_RAND_DOUBLE_TRANSFORM;
76 return gst_fast_random_double ();
82 gst_fast_random_double_range (gdouble start, gdouble end)
84 return gst_fast_random_double () * (end - start) + start;
87 #undef GST_RAND_DOUBLE_TRANSFORM
89 #endif /* __GST_FAST_RANDOM__ */