6563be7ed29ef2d41a090a9ed61caf0e5aa36edc
[xscreensaver] / utils / yarandom.h
1 /* xscreensaver, Copyright (c) 1997 by Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #ifndef __YARANDOM_H__
13 #define __YARANDOM_H__
14
15 #undef random
16 #undef rand
17 #undef drand48
18 #undef srandom
19 #undef srand
20 #undef srand48
21 #undef frand
22
23 #ifdef VMS
24 # include "vms-gtod.h"
25 #endif
26
27 #define random()   ya_random()
28 #define srandom(i) ya_rand_init(0)
29
30 extern unsigned int ya_random (void);
31 extern void ya_rand_init (unsigned int);
32
33
34 #if defined (__GNUC__) && (__GNUC__ >= 2)
35  /* Implement frand using GCC's statement-expression extension. */
36
37 # define frand(f)                                                       \
38   ({ double tmp = (((double) random()) /                                \
39                    (((double) ((unsigned int)~0)) / ((double) (f))));   \
40      tmp < 0 ? (-tmp) : tmp; })
41
42 #else /* not GCC2 - implement frand using a global variable.*/
43
44 static double _frand_tmp_;
45 # define frand(f)                                                       \
46   (_frand_tmp_ = (((double) random()) /                                 \
47                   (((double) ((unsigned int)~0)) / ((double) (f)))),    \
48    _frand_tmp_ < 0 ? (-_frand_tmp_) : _frand_tmp_)
49
50 #endif /* not GCC2 */
51
52 #endif /* __YARANDOM_H__ */