This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH][BZ 14561] Separate rand and random seeds.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Mon, 20 May 2013 21:06:28 +0200
- Subject: [PATCH][BZ 14561] Separate rand and random seeds.
Hi,
Functions rand calls random which causes seed to be shared between them.
This patch causes rand and random use separate seeds.
Comments?
Ondra
* stdlib/rand.c: Include stdlib/random.c
* stdlib/random.c: Separate rand and random.
---
stdlib/rand.c | 14 ++++++--------
stdlib/random.c | 17 +++++++++++++++--
2 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/stdlib/rand.c b/stdlib/rand.c
index 3e0839f..ec10afd 100644
--- a/stdlib/rand.c
+++ b/stdlib/rand.c
@@ -15,14 +15,12 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
-#include <stdlib.h>
-
-#undef rand
/* Return a random integer between 0 and RAND_MAX. */
-int
-rand ()
-{
- return (int) __random ();
-}
+
+#define srandom srand
+#define __srandom __srand
+#define USE_AS_RAND
+
+#include "stdlib/random.c"
diff --git a/stdlib/random.c b/stdlib/random.c
index 967dec3..ed16abd 100644
--- a/stdlib/random.c
+++ b/stdlib/random.c
@@ -191,6 +191,8 @@ static struct random_data unsafe_state =
.end_ptr = &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
};
+
+
/* POSIX.1c requires that there is mutual exclusion for the `rand' and
`srand' functions to prevent concurrent calls from modifying common
data. */
@@ -214,7 +216,9 @@ __srandom (x)
}
weak_alias (__srandom, srandom)
-weak_alias (__srandom, srand)
+
+#ifndef USE_AS_RAND
+
/* Initialize the state information in the given array of N bytes for
future random number generation. Based on the number of bytes we
@@ -277,6 +281,9 @@ __setstate (arg_state)
weak_alias (__setstate, setstate)
+
+#endif
+
/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
same in all the other cases due to all the global variables that have been
@@ -288,8 +295,13 @@ weak_alias (__setstate, setstate)
rear pointers can't wrap on the same call by not testing the rear
pointer if the front one has wrapped. Returns a 31-bit random number. */
+#ifdef USE_AS_RAND
+int
+rand ()
+#else
long int
__random ()
+#endif
{
int32_t retval;
@@ -301,5 +313,6 @@ __random ()
return retval;
}
-
+#ifndef USE_AS_RAND
weak_alias (__random, random)
+#endif
--
1.7.4.4