1 /* xscreensaver, Copyright (c) 1997, 1998, 2003 by Jamie Zawinski <jwz@jwz.org>
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
12 #ifndef __YARANDOM_H__
13 #define __YARANDOM_H__
19 #ifdef HAVE_INTTYPES_H
20 # include <inttypes.h>
33 # include "vms-gtod.h"
36 extern unsigned int ya_random (void);
37 extern void ya_rand_init (unsigned int);
39 #define random() ya_random()
40 #define RAND_MAX 0xFFFFFFFF
42 /*#define srandom(i) ya_rand_init(0)*/
44 /* Define these away to keep people from using the wrong APIs in xscreensaver.
46 #define rand __ERROR_use_random_not_rand_in_xscreensaver__
47 #define drand48 __ERROR_use_frand_not_drand48_in_xscreensaver__
48 #define srandom __ERROR_do_not_call_srandom_in_xscreensaver__
49 #define srand __ERROR_do_not_call_srand_in_xscreensaver__
50 #define srand48 __ERROR_do_not_call_srand48_in_xscreensaver__
51 #define ya_rand_init __ERROR_do_not_call_ya_rand_init_in_xscreensaver__
54 #if defined (__GNUC__) && (__GNUC__ >= 2)
55 /* Implement frand using GCC's statement-expression extension. */
59 ({ double tmp = ((((double) random()) * ((double) (f))) / \
60 ((double) ((unsigned int)~0))); \
61 tmp < 0 ? (-tmp) : tmp; })
63 #else /* not GCC2 - implement frand using a global variable.*/
65 static double _frand_tmp_;
67 (_frand_tmp_ = ((((double) random()) * ((double) (f))) / \
68 ((double) ((unsigned int)~0))), \
69 _frand_tmp_ < 0 ? (-_frand_tmp_) : _frand_tmp_)
73 /* Compatibility with the xlockmore RNG API
74 (note that the xlockmore hacks never expect negative numbers.)
76 #define LRAND() ((long) (random() & 0x7fffffff))
78 /* The first NRAND(n) is much faster, when uint64_t is available to allow it.
79 * Especially on ARM and other processors without built-in division.
81 * n must be greater than zero.
83 * The division by RAND_MAX+1 should optimize down to a bit shift.
85 * Although the result here is never negative, this needs to return signed
86 * integers: A binary operator in C requires operands have the same type, and
87 * if one side is signed and the other is unsigned, but they're both the same
88 * size, then the signed operand is cast to unsigned, and the result is
89 * similarly unsigned. This can potentially cause problems when idioms like
90 * "NRAND(n) * RANDSIGN()" (where RANDSIGN() returns either -1 or 1) is used
91 * to get random numbers from the range (-n,n).
93 #ifdef HAVE_INTTYPES_H
94 # define NRAND(n) ((int32_t) ((uint64_t) random() * (uint32_t) (n) / \
95 ((uint64_t) RAND_MAX + 1)))
97 # define NRAND(n) ((int) (LRAND() % (n)))
100 #define MAXRAND (2147483648.0) /* unsigned 1<<31 as a float */
101 #define SRAND(n) /* already seeded by screenhack.c */
103 #endif /* __YARANDOM_H__ */