about summary refs log tree commit diff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/Makefile10
-rw-r--r--stdlib/a64l.c54
-rw-r--r--stdlib/drand48-iter.c102
-rw-r--r--stdlib/drand48.c33
-rw-r--r--stdlib/drand48_r.c37
-rw-r--r--stdlib/erand48.c34
-rw-r--r--stdlib/erand48_r.c52
-rw-r--r--stdlib/jrand48.c34
-rw-r--r--stdlib/jrand48_r.c49
-rw-r--r--stdlib/l64a.c51
-rw-r--r--stdlib/lcong48.c30
-rw-r--r--stdlib/lcong48_r.c41
-rw-r--r--stdlib/lrand48.c33
-rw-r--r--stdlib/lrand48_r.c32
-rw-r--r--stdlib/mrand48.c33
-rw-r--r--stdlib/mrand48_r.c32
-rw-r--r--stdlib/nrand48.c34
-rw-r--r--stdlib/nrand48_r.c46
-rw-r--r--stdlib/random.c187
-rw-r--r--stdlib/random_r.c337
-rw-r--r--stdlib/seed48.c32
-rw-r--r--stdlib/seed48_r.c41
-rw-r--r--stdlib/srand48.c30
-rw-r--r--stdlib/srand48_r.c52
-rw-r--r--stdlib/stdlib.h109
25 files changed, 1371 insertions, 154 deletions
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 3ea206f623..a28d2a8127 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -32,10 +32,16 @@ routines	:=							      \
 	abs labs							      \
 	div ldiv							      \
 	mblen mbstowcs mbtowc wcstombs wctomb				      \
-	random rand 							      \
+	random random_r rand 						      \
+	drand48 erand48 lrand48 nrand48 mrand48 jrand48			      \
+	srand48 seed48 lcong48						      \
+	drand48_r erand48_r lrand48_r nrand48_r mrand48_r jrand48_r	      \
+	srand48_r seed48_r lcong48_r					      \
+	drand48-iter							      \
 	strtol strtoul strtoq strtouq					      \
 	strtof strtod strtold						      \
-	system
+	system								      \
+	a64l l64a
 
 distribute	:= exit.h grouping.h
 tests		:= tst-strtol tst-strtod testmb testrand testsort testdiv
diff --git a/stdlib/a64l.c b/stdlib/a64l.c
new file mode 100644
index 0000000000..3fdaab513b
--- /dev/null
+++ b/stdlib/a64l.c
@@ -0,0 +1,54 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+long
+a64l (string)
+     const char *string;
+{
+  int cnt;
+  long result = 0l;
+
+  for (cnt = 0; cnt < 6; ++cnt)
+    {
+      result <<= 6;
+      switch (string[cnt])
+	{
+	case '.':
+	  break;
+	case '/':
+	  result |= 1;
+	  break;
+	case '0' ... '9':
+	  result |= 2 + string[cnt] - '0';
+	  break;
+	case 'A' ... 'Z':
+	  result |= 12 + string[cnt] - 'A';
+	  break;
+	case 'a' ... 'z':
+	  result |= 38 + string[cnt] - 'a';
+	  break;
+	default:
+	  return result >> 6;
+	}
+    }
+
+  return result;
+}
diff --git a/stdlib/drand48-iter.c b/stdlib/drand48-iter.c
new file mode 100644
index 0000000000..013dbe792f
--- /dev/null
+++ b/stdlib/drand48-iter.c
@@ -0,0 +1,102 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <stdlib.h>
+
+
+/* Global state for non-reentrent functions.  */
+struct drand48_data __libc_drand48_data;
+
+
+int
+__drand48_iterate (xsubi, buffer)
+     unsigned short int xsubi[3];
+     struct drand48_data *buffer;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (xsubi == NULL || buffer == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Initialize buffer, if not yet done.  */
+  if (!buffer->init)
+    {
+      if (sizeof (unsigned short int) == 2)
+	{
+	  buffer->a[2] = 0x5;
+	  buffer->a[1] = 0xdeec;
+	  buffer->a[0] = 0xe66d;
+	}
+      else
+	{
+	  buffer->a[2] = 0x5deec;
+	  buffer->a[1] = 0xe66d0000;
+	  buffer->a[0] = 0;
+	}
+      buffer->c = 0xb;
+      buffer->init = 1;
+    }
+
+  /* Do the real work.  We choose a data type which contains at least
+     48 bits.  Because we compute the modulus it does not care how
+     many bits really are computed.  */
+
+  if (sizeof (long int) >= 6)
+    {
+      /* The `long' data type is sufficent.  */
+      unsigned long int X, a, result;
+
+#define ONE_STEP							    \
+      if (sizeof (unsigned short int) == 2)				    \
+	{								    \
+	  X = (xsubi[2] << 16 | xsubi[1]) << 16 | xsubi[0];		    \
+	  a = (buffer->a[2] << 16 | buffer->a[1]) << 16 | buffer->a[0];	    \
+									    \
+	  result = X * a + buffer->c;					    \
+									    \
+	  xsubi[0] = result & 0xffff;					    \
+	  result >>= 16;						    \
+	  xsubi[1] = result & 0xffff;					    \
+	  result >>= 16;						    \
+	  xsubi[2] = result & 0xffff;					    \
+	}								    \
+      else								    \
+	{								    \
+	  X = xsubi[2] << 16 | xsubi[1] >> 16;				    \
+	  a = buffer->a[2] << 16 | buffer->a[1] >> 16;			    \
+									    \
+	  result = X * a + buffer->c;					    \
+									    \
+	  xsubi[0] = result >> 16 & 0xffffffffl;			    \
+	  xsubi[1] = result << 16 & 0xffff0000l;			    \
+	}
+      ONE_STEP;
+    }
+  else
+    {
+      /* We have to use the `long long' data type.  */
+      unsigned long long int X, a, result;
+      ONE_STEP;
+    }
+
+  return 0;
+}
diff --git a/stdlib/drand48.c b/stdlib/drand48.c
new file mode 100644
index 0000000000..e2d8450044
--- /dev/null
+++ b/stdlib/drand48.c
@@ -0,0 +1,33 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+double
+drand48 ()
+{
+  double result;
+
+  (void) erand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/drand48_r.c b/stdlib/drand48_r.c
new file mode 100644
index 0000000000..eaba057fa1
--- /dev/null
+++ b/stdlib/drand48_r.c
@@ -0,0 +1,37 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <math.h>
+#include <stdlib.h>
+
+int
+drand48_r (buffer, result)
+     struct drand48_data *buffer;
+     double *result;
+{
+  /* be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  return erand48_r (buffer->X, buffer, result);
+}
diff --git a/stdlib/erand48.c b/stdlib/erand48.c
new file mode 100644
index 0000000000..b63c3bddea
--- /dev/null
+++ b/stdlib/erand48.c
@@ -0,0 +1,34 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+double
+erand48 (xsubi)
+     unsigned short int xsubi[3];
+{
+  double result;
+
+  (void) erand48_r (xsubi, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/erand48_r.c b/stdlib/erand48_r.c
new file mode 100644
index 0000000000..86d2f734d9
--- /dev/null
+++ b/stdlib/erand48_r.c
@@ -0,0 +1,52 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <math.h>
+#include <stdlib.h>
+
+int
+erand48_r (xsubi, buffer, result)
+     unsigned short int xsubi[3];
+     struct drand48_data *buffer;
+     double *result;
+{
+  int i;
+
+  /* Be generous for the arguments, detect some errors.  */
+  if (result == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Compute next state.  */
+  if (__drand48_iterate (xsubi, buffer) < 0)
+    return -1;
+
+  *result = 0.0;
+  for (i = 4 / sizeof (unsigned short int); i >= 0; --i)
+    {
+      double factor = ldexp (1.0, (i - 6) * sizeof (unsigned short int));
+
+      *result += factor * (double) xsubi[i];
+    }
+
+  return 0;
+}
diff --git a/stdlib/jrand48.c b/stdlib/jrand48.c
new file mode 100644
index 0000000000..bdd62fb241
--- /dev/null
+++ b/stdlib/jrand48.c
@@ -0,0 +1,34 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+long
+jrand48 (xsubi)
+     unsigned short int xsubi[3];
+{
+  long result;
+
+  (void) jrand48_r (xsubi, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/jrand48_r.c b/stdlib/jrand48_r.c
new file mode 100644
index 0000000000..b1a4378028
--- /dev/null
+++ b/stdlib/jrand48_r.c
@@ -0,0 +1,49 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+jrand48_r (xsubi, buffer, result)
+     unsigned short int xsubi[3];
+     struct drand48_data *buffer;
+     long *result;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (result == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Compute next state.  */
+  if (__drand48_iterate (xsubi, buffer) < 0)
+    return -1;
+
+  /* Store the result.  */
+  if (sizeof (unsigned short int) == 2)
+    *result = (xsubi[2] & 0x7fff) | xsubi[1];
+  else
+    *result = xsubi[2] & 0x7fffffffl;
+
+  if (xsubi[2] & (1 << (sizeof (xsubi[2]) * 8 - 1)))
+    *result *= -1;
+
+  return 0;
+}
diff --git a/stdlib/l64a.c b/stdlib/l64a.c
new file mode 100644
index 0000000000..ad19a3b6eb
--- /dev/null
+++ b/stdlib/l64a.c
@@ -0,0 +1,51 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Conversion table.  */
+static const char conv_tab[64] =
+{
+  '.', '/', '0', '1', '2', '3', '4', '5',
+  '6', '7', '8', '9', 'A', 'B', 'C', 'D',
+  'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+  'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
+  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
+  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
+};
+
+const char *
+l64a (n)
+     long n;
+{
+  static char result[7];
+  int cnt;
+
+  result[6] = '\0';
+
+  for (cnt = 5; cnt >= 0; --cnt)
+    {
+      result[cnt] = n & 0x3f;
+      n >>= 6;
+    }
+
+  return result;
+}
diff --git a/stdlib/lcong48.c b/stdlib/lcong48.c
new file mode 100644
index 0000000000..7e4d18806f
--- /dev/null
+++ b/stdlib/lcong48.c
@@ -0,0 +1,30 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+void
+lcong48 (param)
+     unsigned short int param[7];
+{
+  (void) lcong48_r (param, &__libc_drand48_data);
+}
diff --git a/stdlib/lcong48_r.c b/stdlib/lcong48_r.c
new file mode 100644
index 0000000000..cc38b4dc7c
--- /dev/null
+++ b/stdlib/lcong48_r.c
@@ -0,0 +1,41 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+lcong48_r (param, buffer)
+     unsigned short int param[7];
+     struct drand48_data *buffer;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Store the given values.  */
+  memcpy (buffer->X, &param[0], sizeof (buffer->X));
+  memcpy (buffer->a, &param[3], sizeof (buffer->a));
+  buffer->c = param[6];
+  buffer->init = 1;
+
+  return 0;
+}
diff --git a/stdlib/lrand48.c b/stdlib/lrand48.c
new file mode 100644
index 0000000000..a3e6ea1a7d
--- /dev/null
+++ b/stdlib/lrand48.c
@@ -0,0 +1,33 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+long
+lrand48 ()
+{
+  long result;
+
+  (void) nrand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/lrand48_r.c b/stdlib/lrand48_r.c
new file mode 100644
index 0000000000..696722a8cc
--- /dev/null
+++ b/stdlib/lrand48_r.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+lrand48_r (buffer, result)
+     struct drand48_data *buffer;
+     long *result;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+   return -1;
+
+  return nrand48_r (buffer->X, buffer, result);
+}
diff --git a/stdlib/mrand48.c b/stdlib/mrand48.c
new file mode 100644
index 0000000000..e71b23465e
--- /dev/null
+++ b/stdlib/mrand48.c
@@ -0,0 +1,33 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+long
+mrand48 ()
+{
+  long result;
+
+  (void) jrand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/mrand48_r.c b/stdlib/mrand48_r.c
new file mode 100644
index 0000000000..92d1b11121
--- /dev/null
+++ b/stdlib/mrand48_r.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+mrand48_r (buffer, result)
+     struct drand48_data *buffer;
+     long *result;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+   return -1;
+
+  return jrand48_r (buffer->X, buffer, result);
+}
diff --git a/stdlib/nrand48.c b/stdlib/nrand48.c
new file mode 100644
index 0000000000..0dd2c2b34f
--- /dev/null
+++ b/stdlib/nrand48.c
@@ -0,0 +1,34 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+long
+nrand48 (xsubi)
+     unsigned short int xsubi[3];
+{
+  long result;
+
+  (void) nrand48_r (xsubi, &__libc_drand48_data, &result);
+
+  return result;
+}
diff --git a/stdlib/nrand48_r.c b/stdlib/nrand48_r.c
new file mode 100644
index 0000000000..1bbc29e7ef
--- /dev/null
+++ b/stdlib/nrand48_r.c
@@ -0,0 +1,46 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+nrand48_r (xsubi, buffer, result)
+     unsigned short int xsubi[3];
+     struct drand48_data *buffer;
+     long *result;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (result == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Compute next state.  */
+  if (__drand48_iterate (xsubi, buffer) < 0)
+    return -1;
+
+  /* Store the result.  */
+  if (sizeof (unsigned short int) == 2)
+    *result = xsubi[2] << 15 | xsubi[1] >> 1;
+  else
+    *result = xsubi[2] >> 1;
+
+  return 0;
+}
diff --git a/stdlib/random.c b/stdlib/random.c
index 473a5b13d3..c3f8eaa0a3 100644
--- a/stdlib/random.c
+++ b/stdlib/random.c
@@ -19,10 +19,9 @@
  * This is derived from the Berkeley source:
  *	@(#)random.c	5.5 (Berkeley) 7/6/88
  * It was reworked for the GNU C Library by Roland McGrath.
+ * Rewritten to use reentrent functions by Ulrich Drepper, 1995.
  */
 
-#include <ansidecl.h>
-#include <errno.h>
 #include <limits.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -105,10 +104,6 @@
 
 #define	MAX_TYPES	5	/* Max number of types above.  */
 
-static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
-static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
-
-
 
 /* Initially, everything is set up as if from:
 	initstate(1, randtbl, 128);
@@ -132,6 +127,9 @@ static long int randtbl[DEG_3 + 1] =
     -205601318, 
   };
 
+
+static struct random_data unsafe_state =
+  {
 /* FPTR and RPTR are two pointers into the state info, a front and a rear
    pointer.  These two pointers are always rand_sep places aparts, as they
    cycle through the state information.  (Yes, this does mean we could get
@@ -142,10 +140,8 @@ static long int randtbl[DEG_3 + 1] =
    in the initialization of randtbl) because the state table pointer is set
    to point to randtbl[1] (as explained below).)  */
 
-static long int *fptr = &randtbl[SEP_3 + 1];
-static long int *rptr = &randtbl[1];
-
-
+    fptr : &randtbl[SEP_3 + 1],
+    rptr : &randtbl[1],
 
 /* The following things are the pointer to the state information table,
    the type of the current generator, the degree of the current polynomial
@@ -157,13 +153,14 @@ static long int *rptr = &randtbl[1];
    indexing every time to find the address of the last element to see if
    the front and rear pointers have wrapped.  */
 
-static long int *state = &randtbl[1];
+    state : &randtbl[1],
 
-static int rand_type = TYPE_3;
-static int rand_deg = DEG_3;
-static int rand_sep = SEP_3;
+    rand_type : TYPE_3,
+    rand_deg : DEG_3,
+    rand_sep : SEP_3,
 
-static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])];
+    end_ptr : &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
+};
 
 /* Initialize the random number generator based on the given seed.  If the
    type is the trivial no-state-information type, just remember the seed.
@@ -174,27 +171,10 @@ static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])];
    introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
    for default usage relies on values produced by this routine.  */
 void
-DEFUN(__srandom, (x), unsigned int x)
+__srandom (x)
+     unsigned int x;
 {
-  state[0] = x;
-  if (rand_type != TYPE_0)
-    {
-      register long int i;
-      for (i = 1; i < rand_deg; ++i)
-	{
-	  /* This does:
-	       state[i] = (16807 * state[i - 1]) % 2147483647;
-	     but avoids overflowing 31 bits.  */
-	  long int hi = state[i - 1] / 127773;
-	  long int lo = state[i - 1] % 127773;
-	  long int test = 16807 * lo - 2836 * hi;
-	  state[i] = test + (test < 0 ? 2147483647 : 0);
-	}
-      fptr = &state[rand_sep];
-      rptr = &state[0];
-      for (i = 0; i < 10 * rand_deg; ++i)
-	(void) __random ();
-    }
+  (void) __srandom_r (x, &unsafe_state);
 }
 
 weak_alias (__srandom, srandom)
@@ -211,60 +191,15 @@ weak_alias (__srandom, srand)
    Note: The first thing we do is save the current state, if any, just like
    setstate so that it doesn't matter when initstate is called.
    Returns a pointer to the old state.  */
-PTR
-DEFUN(__initstate, (seed, arg_state, n),
-      unsigned int seed AND PTR arg_state AND size_t n)
+void *
+__initstate (seed, arg_state, n)
+     unsigned int seed;
+     void *arg_state;
+     size_t n;
 {
-  PTR ostate = (PTR) &state[-1];
-
-  if (rand_type == TYPE_0)
-    state[-1] = rand_type;
-  else
-    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
-  if (n < BREAK_1)
-    {
-      if (n < BREAK_0)
-	{
-	  errno = EINVAL;
-	  return NULL;
-	}
-      rand_type = TYPE_0;
-      rand_deg = DEG_0;
-      rand_sep = SEP_0;
-    }
-  else if (n < BREAK_2)
-    {
-      rand_type = TYPE_1;
-      rand_deg = DEG_1;
-      rand_sep = SEP_1;
-    }
-  else if (n < BREAK_3)
-    {
-      rand_type = TYPE_2;
-      rand_deg = DEG_2;
-      rand_sep = SEP_2;
-    }
-  else if (n < BREAK_4)
-    {
-      rand_type = TYPE_3;
-      rand_deg = DEG_3;
-      rand_sep = SEP_3;
-    }
-  else
-    {
-      rand_type = TYPE_4;
-      rand_deg = DEG_4;
-      rand_sep = SEP_4;
-    }
-
-  state = &((long int *) arg_state)[1];	/* First location.  */
-  /* Must set END_PTR before srandom.  */
-  end_ptr = &state[rand_deg];
-  __srandom(seed);
-  if (rand_type == TYPE_0)
-    state[-1] = rand_type;
-  else
-    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
+  void *ostate = (void *) &unsafe_state.state[-1];
+
+  __initstate_r (seed, arg_state, n, &unsafe_state);
 
   return ostate;
 }
@@ -279,44 +214,14 @@ weak_alias (__initstate, initstate)
    to the order in which things are done, it is OK to call setstate with the
    same state as the current state
    Returns a pointer to the old state information.  */
-PTR
-DEFUN(__setstate, (arg_state), PTR arg_state)
+void *
+__setstate (arg_state)
+     void *arg_state;
 {
-  register long int *new_state = (long int *) arg_state;
-  register int type = new_state[0] % MAX_TYPES;
-  register int rear = new_state[0] / MAX_TYPES;
-  PTR ostate = (PTR) &state[-1];
-
-  if (rand_type == TYPE_0)
-    state[-1] = rand_type;
-  else
-    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
-
-  switch (type)
-    {
-    case TYPE_0:
-    case TYPE_1:
-    case TYPE_2:
-    case TYPE_3:
-    case TYPE_4:
-      rand_type = type;
-      rand_deg = degrees[type];
-      rand_sep = seps[type];
-      break;
-    default:
-      /* State info munged.  */
-      errno = EINVAL;
-      return NULL;
-    }
-
-  state = &new_state[1];
-  if (rand_type != TYPE_0)
-    {
-      rptr = &state[rear];
-      fptr = &state[(rear + rand_sep) % rand_deg];
-    }
-  /* Set end_ptr too.  */
-  end_ptr = &state[rand_deg];
+  void *ostate = (void *) &unsafe_state.state[-1];
+
+  if (__setstate_r (arg_state, &unsafe_state) < 0)
+    return NULL;
 
   return ostate;
 }
@@ -335,33 +240,13 @@ weak_alias (__setstate, setstate)
    pointer if the front one has wrapped.  Returns a 31-bit random number.  */
 
 long int
-DEFUN_VOID(__random)
+__random ()
 {
-  if (rand_type == TYPE_0)
-    {
-      state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX;
-      return state[0];
-    }
-  else
-    {
-      long int i;
-      *fptr += *rptr;
-      /* Chucking least random bit.  */
-      i = (*fptr >> 1) & LONG_MAX;
-      ++fptr;
-      if (fptr >= end_ptr)
-	{
-	  fptr = state;
-	  ++rptr;
-	}
-      else
-	{
-	  ++rptr;
-	  if (rptr >= end_ptr)
-	    rptr = state;
-	}
-      return i;
-    }
+  long int retval;
+
+  (void) __random_r (&unsafe_state, &retval);
+
+  return retval;
 }
 
 weak_alias (__random, random)
diff --git a/stdlib/random_r.c b/stdlib/random_r.c
new file mode 100644
index 0000000000..aa7a33fa6b
--- /dev/null
+++ b/stdlib/random_r.c
@@ -0,0 +1,337 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+/*
+ * This is derived from the Berkeley source:
+ *	@(#)random.c	5.5 (Berkeley) 7/6/88
+ * It was reworked for the GNU C Library by Roland McGrath.
+ * Rewritten to be reentrent by Ulrich Drepper, 1995
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+
+/* An improved random number generation package.  In addition to the standard
+   rand()/srand() like interface, this package also has a special state info
+   interface.  The initstate() routine is called with a seed, an array of
+   bytes, and a count of how many bytes are being passed in; this array is
+   then initialized to contain information for random number generation with
+   that much state information.  Good sizes for the amount of state
+   information are 32, 64, 128, and 256 bytes.  The state can be switched by
+   calling the setstate() function with the same array as was initiallized
+   with initstate().  By default, the package runs with 128 bytes of state
+   information and generates far better random numbers than a linear
+   congruential generator.  If the amount of state information is less than
+   32 bytes, a simple linear congruential R.N.G. is used.  Internally, the
+   state information is treated as an array of longs; the zeroeth element of
+   the array is the type of R.N.G. being used (small integer); the remainder
+   of the array is the state information for the R.N.G.  Thus, 32 bytes of
+   state information will give 7 longs worth of state information, which will
+   allow a degree seven polynomial.  (Note: The zeroeth word of state
+   information also has some other information stored in it; see setstate
+   for details).  The random number generation technique is a linear feedback
+   shift register approach, employing trinomials (since there are fewer terms
+   to sum up that way).  In this approach, the least significant bit of all
+   the numbers in the state table will act as a linear feedback shift register,
+   and will have period 2^deg - 1 (where deg is the degree of the polynomial
+   being used, assuming that the polynomial is irreducible and primitive).
+   The higher order bits will have longer periods, since their values are
+   also influenced by pseudo-random carries out of the lower bits.  The
+   total period of the generator is approximately deg*(2**deg - 1); thus
+   doubling the amount of state information has a vast influence on the
+   period of the generator.  Note: The deg*(2**deg - 1) is an approximation
+   only good for large deg, when the period of the shift register is the
+   dominant factor.  With deg equal to seven, the period is actually much
+   longer than the 7*(2**7 - 1) predicted by this formula.  */
+
+
+
+/* For each of the currently supported random number generators, we have a
+   break value on the amount of state information (you need at least thi
+   bytes of state info to support this random number generator), a degree for
+   the polynomial (actually a trinomial) that the R.N.G. is based on, and
+   separation between the two lower order coefficients of the trinomial.  */
+
+/* Linear congruential.  */
+#define	TYPE_0		0
+#define	BREAK_0		8
+#define	DEG_0		0
+#define	SEP_0		0
+
+/* x**7 + x**3 + 1.  */
+#define	TYPE_1		1
+#define	BREAK_1		32
+#define	DEG_1		7
+#define	SEP_1		3
+
+/* x**15 + x + 1.  */
+#define	TYPE_2		2
+#define	BREAK_2		64
+#define	DEG_2		15
+#define	SEP_2		1
+
+/* x**31 + x**3 + 1.  */
+#define	TYPE_3		3
+#define	BREAK_3		128
+#define	DEG_3		31
+#define	SEP_3		3
+
+/* x**63 + x + 1.  */
+#define	TYPE_4		4
+#define	BREAK_4		256
+#define	DEG_4		63
+#define	SEP_4		1
+
+
+/* Array versions of the above information to make code run faster.
+   Relies on fact that TYPE_i == i.  */
+
+#define	MAX_TYPES	5	/* Max number of types above.  */
+
+static const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
+static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
+
+
+
+
+/* Initialize the random number generator based on the given seed.  If the
+   type is the trivial no-state-information type, just remember the seed.
+   Otherwise, initializes state[] based on the given "seed" via a linear
+   congruential generator.  Then, the pointers are set to known locations
+   that are exactly rand_sep places apart.  Lastly, it cycles the state
+   information a given number of times to get rid of any initial dependencies
+   introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
+   for default usage relies on values produced by this routine.  */
+int
+__srandom_r (x, buf)
+     unsigned int x;
+     struct random_data *buf;
+{
+  if (buf == NULL || buf->rand_type < TYPE_0 || buf->rand_type > TYPE_4)
+    return -1;
+
+  buf->state[0] = x;
+  if (buf->rand_type != TYPE_0)
+    {
+      long int i;
+      for (i = 1; i < buf->rand_deg; ++i)
+	{
+	  /* This does:
+	       state[i] = (16807 * state[i - 1]) % 2147483647;
+	     but avoids overflowing 31 bits.  */
+	  long int hi = buf->state[i - 1] / 127773;
+	  long int lo = buf->state[i - 1] % 127773;
+	  long int test = 16807 * lo - 2836 * hi;
+	  buf->state[i] = test + (test < 0 ? 2147483647 : 0);
+	}
+      buf->fptr = &buf->state[buf->rand_sep];
+      buf->rptr = &buf->state[0];
+      for (i = 0; i < 10 * buf->rand_deg; ++i)
+	{
+	  long int discard;
+	  (void) __random_r (buf, &discard);
+	}
+    }
+
+  return 0;
+}
+
+weak_alias (__srandom_r, srandom_r)
+weak_alias (__srandom_r, srand_r)
+
+/* Initialize the state information in the given array of N bytes for
+   future random number generation.  Based on the number of bytes we
+   are given, and the break values for the different R.N.G.'s, we choose
+   the best (largest) one we can and set things up for it.  srandom is
+   then called to initialize the state information.  Note that on return
+   from srandom, we set state[-1] to be the type multiplexed with the current
+   value of the rear pointer; this is so successive calls to initstate won't
+   lose this information and will be able to restart with setstate.
+   Note: The first thing we do is save the current state, if any, just like
+   setstate so that it doesn't matter when initstate is called.
+   Returns a pointer to the old state.  */
+int
+__initstate_r (seed, arg_state, n, buf)
+     unsigned int seed;
+     void *arg_state;
+     size_t n;
+     struct random_data *buf;
+{
+  if (buf == NULL)
+    return -1;
+
+  if (buf->rand_type == TYPE_0)
+    buf->state[-1] = buf->rand_type;
+  else
+    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
+  if (n < BREAK_1)
+    {
+      if (n < BREAK_0)
+	{
+	  errno = EINVAL;
+	  return -1;
+	}
+      buf->rand_type = TYPE_0;
+      buf->rand_deg = DEG_0;
+      buf->rand_sep = SEP_0;
+    }
+  else if (n < BREAK_2)
+    {
+      buf->rand_type = TYPE_1;
+      buf->rand_deg = DEG_1;
+      buf->rand_sep = SEP_1;
+    }
+  else if (n < BREAK_3)
+    {
+      buf->rand_type = TYPE_2;
+      buf->rand_deg = DEG_2;
+      buf->rand_sep = SEP_2;
+    }
+  else if (n < BREAK_4)
+    {
+      buf->rand_type = TYPE_3;
+      buf->rand_deg = DEG_3;
+      buf->rand_sep = SEP_3;
+    }
+  else
+    {
+      buf->rand_type = TYPE_4;
+      buf->rand_deg = DEG_4;
+      buf->rand_sep = SEP_4;
+    }
+
+  buf->state = &((long int *) arg_state)[1];	/* First location.  */
+  /* Must set END_PTR before srandom.  */
+  buf->end_ptr = &buf->state[buf->rand_deg];
+
+  __srandom_r (seed, buf);
+
+  if (buf->rand_type == TYPE_0)
+    buf->state[-1] = buf->rand_type;
+  else
+    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
+
+  return 0;
+}
+
+weak_alias (__initstate_r, initstate_r)
+
+/* Restore the state from the given state array.
+   Note: It is important that we also remember the locations of the pointers
+   in the current state information, and restore the locations of the pointers
+   from the old state information.  This is done by multiplexing the pointer
+   location into the zeroeth word of the state information. Note that due
+   to the order in which things are done, it is OK to call setstate with the
+   same state as the current state
+   Returns a pointer to the old state information.  */
+int
+__setstate_r (arg_state, buf)
+     void *arg_state;
+     struct random_data *buf;
+{
+  long int *new_state = (long int *) arg_state;
+  int type = new_state[0] % MAX_TYPES;
+  int rear = new_state[0] / MAX_TYPES;
+
+  if (buf == NULL)
+    return -1;
+
+  if (buf->rand_type == TYPE_0)
+    buf->state[-1] = buf->rand_type;
+  else
+    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
+
+  switch (type)
+    {
+    case TYPE_0:
+    case TYPE_1:
+    case TYPE_2:
+    case TYPE_3:
+    case TYPE_4:
+      buf->rand_type = type;
+      buf->rand_deg = degrees[type];
+      buf->rand_sep = seps[type];
+      break;
+    default:
+      /* State info munged.  */
+      errno = EINVAL;
+      return -1;
+    }
+
+  buf->state = &new_state[1];
+  if (buf->rand_type != TYPE_0)
+    {
+      buf->rptr = &buf->state[rear];
+      buf->fptr = &buf->state[(rear + buf->rand_sep) % buf->rand_deg];
+    }
+  /* Set end_ptr too.  */
+  buf->end_ptr = &buf->state[buf->rand_deg];
+
+  return 0;
+}
+
+weak_alias (__setstate_r, setstate_r)
+
+/* 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 ther other cases due to all the global variables that have been
+   set up.  The basic operation is to add the number at the rear pointer into
+   the one at the front pointer.  Then both pointers are advanced to the next
+   location cyclically in the table.  The value returned is the sum generated,
+   reduced to 31 bits by throwing away the "least random" low bit.
+   Note: The code takes advantage of the fact that both the front and
+   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.  */
+
+int
+__random_r (buf, result)
+     struct random_data *buf;
+     long int *result;
+{
+  if (buf == NULL || result == NULL)
+    return -1;
+
+  if (buf->rand_type == TYPE_0)
+    {
+      buf->state[0] = ((buf->state[0] * 1103515245) + 12345) & LONG_MAX;
+      *result = buf->state[0];
+    }
+  else
+    {
+      *buf->fptr += *buf->rptr;
+      /* Chucking least random bit.  */
+      *result = (*buf->fptr >> 1) & LONG_MAX;
+      ++buf->fptr;
+      if (buf->fptr >= buf->end_ptr)
+	{
+	  buf->fptr = buf->state;
+	  ++buf->rptr;
+	}
+      else
+	{
+	  ++buf->rptr;
+	  if (buf->rptr >= buf->end_ptr)
+	    buf->rptr = buf->state;
+	}
+    }
+  return 0;
+}
+
+weak_alias (__random_r, random_r)
diff --git a/stdlib/seed48.c b/stdlib/seed48.c
new file mode 100644
index 0000000000..634f9a72fc
--- /dev/null
+++ b/stdlib/seed48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+unsigned short int *
+seed48 (seed16v)
+     unsigned short int seed16v[3];
+{
+  (void) seed48_r (seed16v, &__libc_drand48_data);
+
+  return __libc_drand48_data.old_X;
+}
diff --git a/stdlib/seed48_r.c b/stdlib/seed48_r.c
new file mode 100644
index 0000000000..4d43507742
--- /dev/null
+++ b/stdlib/seed48_r.c
@@ -0,0 +1,41 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+seed48_r (seed16v, buffer)
+     unsigned short int seed16v[3];
+     struct drand48_data *buffer;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* Save old value at a private place to be used as return value.  */
+  memcpy (buffer->old_X, buffer->X, sizeof (buffer->X));
+
+  /* Install new state.  */
+  memcpy (buffer->X, seed16v, sizeof (buffer->X));
+
+  return 0;
+}
diff --git a/stdlib/srand48.c b/stdlib/srand48.c
new file mode 100644
index 0000000000..80ada310c3
--- /dev/null
+++ b/stdlib/srand48.c
@@ -0,0 +1,30 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrent functions.  Defined in drand48-iter.c.  */
+extern struct drand48_data __libc_drand48_data;
+
+void
+srand48 (seedval)
+     long seedval;
+{
+  (void) srand48_r (seedval, &__libc_drand48_data);
+}
diff --git a/stdlib/srand48_r.c b/stdlib/srand48_r.c
new file mode 100644
index 0000000000..7e77d1675a
--- /dev/null
+++ b/stdlib/srand48_r.c
@@ -0,0 +1,52 @@
+/* Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+
+int
+srand48_r (seedval, buffer)
+     long seedval;
+     struct drand48_data *buffer;
+{
+  /* Be generous for the arguments, detect some errors.  */
+  if (buffer == NULL)
+    {
+      errno = EFAULT;
+      return -1;
+    }
+
+  /* The standards say we only have 32 bits.  */
+  if (sizeof (long) > 4)
+    seedval &= 0xffffffffl;
+
+  if (sizeof (unsigned short int) == 2)
+    {
+      buffer->X[2] = seedval >> 16;
+      buffer->X[1] = seedval & 0xffffl;
+      buffer->X[0] = 0x330e;
+    }
+  else
+    {
+      buffer->X[2] = seedval;
+      buffer->X[1] = 0x330e0000;
+      buffer->X[0] = 0;
+    }
+
+  return 0;
+}
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
index 2a3cf8ec5f..c6c504a143 100644
--- a/stdlib/stdlib.h
+++ b/stdlib/stdlib.h
@@ -198,7 +198,83 @@ extern __inline __ptr_t initstate (unsigned int __seed,
 extern __inline __ptr_t setstate (__ptr_t __statebuf)
 { return __setstate (__statebuf); }
 #endif /* Optimizing GCC >=2.  */
-#endif /* Use BSD.  */
+
+#ifdef __USE_REENTRANT
+/* Reentrant versions of the `random' family of functions.
+   These functions all use the following data structure to contain
+   state, rather than global state variables.  */
+
+struct random_data
+  {
+    long int *fptr;		/* Front pointer.  */
+    long int *rptr;		/* Rear pointer.  */
+    long int *state;		/* Array of state values.  */
+    int rand_type;		/* Type of random number generator.  */
+    int rand_deg;		/* Degree of random number generator.  */
+    int rand_sep;		/* Distance between front and rear.  */
+    long int *end_ptr;		/* Pointer behind state table.  */
+  };
+
+extern int __random_r __P ((struct random_data *__buf, long int *__result));
+extern int __srandom_r __P ((unsigned int __seed, struct random_data *__buf));
+extern int __initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
+			       size_t __statelen, struct random_data *__buf));
+extern int __setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
+
+extern int random_r __P ((struct random_data *__buf, long int *__result));
+extern int srandom_r __P ((unsigned int __seed, struct random_data *__buf));
+extern int initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
+			     size_t __statelen, struct random_data *__buf));
+extern int setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
+#endif	/* __USE_REENTRANT.  */
+#endif	/* Use BSD.  */
+
+
+#ifdef	__USE_SVID
+/* System V style 48-bit random number generator functions.  */
+
+/* Data structure for communication with thread safe versions.  */
+struct drand48_data
+  {
+    unsigned short int X[3];	/* Current state.  */
+    unsigned short int a[3];	/* Factor in congruential formula.  */
+    unsigned short int c;	/* Additive const. in congruential formula.  */
+    unsigned short int old_X[3]; /* Old state.  */
+    int init;			/* Flag for initializing.  */
+  };
+
+/* Return non-negative, double-precision floating-point value in [0.0,1.0).  */
+extern double drand48 __P ((void));
+extern int drand48_r __P ((struct drand48_data *__buffer, double *__result));
+extern double erand48 __P ((unsigned short int __xsubi[3]));
+extern int erand48_r __P ((unsigned short int __xsubi[3],
+			   struct drand48_data *__buffer, double *__result));
+/* Return non-negative, long integer in [0,2^31).  */
+extern long lrand48 __P ((void));
+extern int lrand48_r __P ((struct drand48_data *__buffer, long *__result));
+extern long nrand48 __P ((unsigned short int __xsubi[3]));
+extern int nrand48_r __P ((unsigned short int __xsubi[3],
+			   struct drand48_data *__buffer, long *__result));
+/* Return signed, long integers in [-2^31,2^31).  */
+extern long mrand48 __P ((void));
+extern int mrand48_r __P ((struct drand48_data *__buffer, long *__result));
+extern long jrand48 __P ((unsigned short int __xsubi[3]));
+extern int jrand48_r __P ((unsigned short int __xsubi[3],
+			   struct drand48_data *__buffer, long *__result));
+/* Seed random number generator.  */
+extern void srand48 __P ((long __seedval));
+extern int srand48_r __P ((long __seedval, struct drand48_data *__buffer));
+extern unsigned short int *seed48 __P ((unsigned short int __seed16v[3]));
+extern int seed48_r __P ((unsigned short int __seed16v[3],
+			  struct drand48_data *__buffer));
+extern void lcong48 __P ((unsigned short int __param[7]));
+extern int lcong48_r __P ((unsigned short int __param[7],
+			   struct drand48_data *__buffer));
+
+/* Internal function to compute next state of the generator.  */
+extern int __drand48_iterate __P ((unsigned short int __xsubi[3],
+				   struct drand48_data *__buffer));
+#endif	/* __USE_SVID.  */
 
 
 /* Allocate SIZE bytes of memory.  */
@@ -268,7 +344,10 @@ extern int system __P ((__const char *__command));
 
 
 /* Shorthand for type of comparison functions.  */
+#ifndef __COMPAR_FN_T
+#define __COMPAR_FN_T
 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
+#endif
 
 #ifdef	__USE_GNU
 typedef __compar_fn_t comparison_fn_t;
@@ -308,6 +387,34 @@ extern __CONSTVALUE div_t div __P ((int __numer, int __denom));
 extern __CONSTVALUE ldiv_t ldiv __P ((long int __numer, long int __denom));
 
 
+#ifdef __USE_SVID
+/* Convert floating point numbers to strings.  The returned values are
+   valid only until another call to the same function.  */
+
+/* Convert VALUE to a string with NDIGIT digits and return a pointer to
+   this.  Set *DECPT with the position of the decimal character and *SIGN
+   with the sign of the number.  */
+char *ecvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
+
+/* Convert VALUE to a string rounded to NDIGIT decimal digits.  Set *DECPT
+   with the position of the decimal character and *SIGN with the sign of
+   the number.  */
+char *fcvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
+
+/* If possible convert VALUE to a string with NDIGIT significant digits.
+   Otherwise use exponential representation.  The resulting string will
+   be written to BUF.  */
+char *gcvt __P ((double __value, int __ndigit, char *__buf));
+
+/* Reentrant version of the functions above which provide their own
+   buffers.  */
+int ecvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
+		 char *__buf, int *__len));
+int fcvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
+		 char *__buf, int *__len));
+#endif
+
+
 /* Return the length of the multibyte character
    in S, which is no longer than N.  */
 extern int mblen __P ((__const char *__s, size_t __n));