diff --git a/configure b/configure
index 3e8d074f8d389e615b424c3b9cf0cd6feedb5bcf..5790a3219c78c436ab258b1f54f6c4b8dfcf0fd7 100755
--- a/configure
+++ b/configure
@@ -18594,7 +18594,8 @@ LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
 
 
-for ac_func in crypt getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul
+
+for ac_func in crypt erand48 getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul
 do
 as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 { $as_echo "$as_me:$LINENO: checking for $ac_func" >&5
@@ -19207,12 +19208,6 @@ case " $LIBOBJS " in
  ;;
 esac
 
-case " $LIBOBJS " in
-  *" rand.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS rand.$ac_objext"
- ;;
-esac
-
 case " $LIBOBJS " in
   *" win32env.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS win32env.$ac_objext"
diff --git a/configure.in b/configure.in
index 2034d7a1677dce7b39bff067f9dec7816223bf40..c713e543649e84da0262ce83722cddf2d1b494ce 100644
--- a/configure.in
+++ b/configure.in
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $PostgreSQL: pgsql/configure.in,v 1.604 2009/07/02 18:55:40 petere Exp $
+dnl $PostgreSQL: pgsql/configure.in,v 1.605 2009/07/16 17:43:52 tgl Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -1249,7 +1249,7 @@ fi
 pgac_save_LIBS="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_REPLACE_FUNCS([crypt getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul])
+AC_REPLACE_FUNCS([crypt erand48 getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul])
 
 case $host_os in
 
@@ -1294,7 +1294,6 @@ if test "$PORTNAME" = "win32"; then
 AC_REPLACE_FUNCS(gettimeofday)
 AC_LIBOBJ(kill)
 AC_LIBOBJ(open)
-AC_LIBOBJ(rand)
 AC_LIBOBJ(win32env)
 AC_LIBOBJ(win32error)
 AC_DEFINE([HAVE_SYMLINK], 1,
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 39ca31225fc2d2998955079d150c769f474962f2..ba807f4275057878d216dbfe50d72cbdc6dbfb8b 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -137,6 +137,9 @@
 /* Define to 1 if you have the <editline/readline.h> header file. */
 #undef HAVE_EDITLINE_READLINE_H
 
+/* Define to 1 if you have the `erand48' function. */
+#undef HAVE_ERAND48
+
 /* Define to 1 if you have the `ERR_set_mark' function. */
 #undef HAVE_ERR_SET_MARK
 
diff --git a/src/include/port.h b/src/include/port.h
index 7eaf84418932e5c02951ce083db4b281b07b60e5..77e6f10a06ee4c087833a0cd303426f358f6672a 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/port.h,v 1.125 2009/06/11 14:49:08 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/port.h,v 1.126 2009/07/16 17:43:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -316,10 +316,6 @@ extern FILE *pgwin32_fopen(const char *, const char *);
 #define popen(a,b) _popen(a,b)
 #define pclose(a) _pclose(a)
 
-/* Missing rand functions */
-extern long lrand48(void);
-extern void srand48(long seed);
-
 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
 #ifndef HAVE_GETTIMEOFDAY
 /* Last parameter not used */
@@ -351,6 +347,13 @@ extern off_t ftello(FILE *stream);
 #endif
 #endif
 
+#ifndef HAVE_ERAND48
+/* we assume all of these are present or missing together */
+extern double erand48(unsigned short xseed[3]);
+extern long lrand48(void);
+extern void srand48(long seed);
+#endif
+
 #ifndef HAVE_FSEEKO
 #define fseeko(a, b, c) fseek(a, b, c)
 #define ftello(a)		ftell(a)
diff --git a/src/port/rand.c b/src/port/erand48.c
similarity index 71%
rename from src/port/rand.c
rename to src/port/erand48.c
index 8559ef8c175fa37dcd5b8680a3318c3be81e6ec8..ce623e2e9655c1d6028e4d2d40e02acc631e3b94 100644
--- a/src/port/rand.c
+++ b/src/port/erand48.c
@@ -1,16 +1,13 @@
-/*
- * $PostgreSQL: pgsql/src/port/rand.c,v 1.6 2009/06/11 14:49:15 momjian Exp $
+/*-------------------------------------------------------------------------
  *
- *-------------------------------------------------------------------------
+ * erand48.c
+ *
+ * This file supplies versions of erand48(), lrand48(), and srand48()
+ * for machines that lack them.  (These are all the members of the drand48
+ * family that Postgres currently requires.  We name the file after erand48
+ * because that is the one that configure tests for.)
  *
- * rand.c
- *	  Missing rand implementations for Win32
  *
- *-------------------------------------------------------------------------
- */
-#include "c.h"
-
-/*
  * Copyright (c) 1993 Martin Birgmeier
  * All rights reserved.
  *
@@ -21,7 +18,17 @@
  * This software is provided ``as is'', and comes with no warranties
  * of any kind. I shall in no event be liable for anything that happens
  * to anyone/anything when using this software.
+ *
+ * IDENTIFICATION
+ *	  $PostgreSQL: pgsql/src/port/erand48.c,v 1.1 2009/07/16 17:43:52 tgl Exp $
+ *
+ *-------------------------------------------------------------------------
  */
+
+#include "c.h"
+
+#include <math.h>
+
 #define RAND48_SEED_0	(0x330e)
 #define RAND48_SEED_1	(0xabcd)
 #define RAND48_SEED_2	(0x1234)
@@ -30,17 +37,18 @@
 #define RAND48_MULT_2	(0x0005)
 #define RAND48_ADD		(0x000b)
 
-unsigned short _rand48_seed[3] = {
+static unsigned short _rand48_seed[3] = {
 	RAND48_SEED_0,
 	RAND48_SEED_1,
 	RAND48_SEED_2
 };
-unsigned short _rand48_mult[3] = {
+static unsigned short _rand48_mult[3] = {
 	RAND48_MULT_0,
 	RAND48_MULT_1,
 	RAND48_MULT_2
 };
-unsigned short _rand48_add = RAND48_ADD;
+static unsigned short _rand48_add = RAND48_ADD;
+
 
 static void
 _dorand48(unsigned short xseed[3])
@@ -62,6 +70,16 @@ _dorand48(unsigned short xseed[3])
 	xseed[2] = (unsigned short) accu;
 }
 
+
+double
+erand48(unsigned short xseed[3])
+{
+	_dorand48(xseed);
+	return ldexp((double) xseed[0], -48) +
+		ldexp((double) xseed[1], -32) +
+		ldexp((double) xseed[2], -16);
+}
+
 long
 lrand48(void)
 {
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index b5d965ba67712d1655b3f004adcc9ee88064ab3f..dfc29ea84448d1029ad7a099fb1ec0a97979fdad 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -3,7 +3,7 @@ package Mkvcbuild;
 #
 # Package that generates build files for msvc build
 #
-# $PostgreSQL: pgsql/src/tools/msvc/Mkvcbuild.pm,v 1.40 2009/06/05 18:29:56 adunstan Exp $
+# $PostgreSQL: pgsql/src/tools/msvc/Mkvcbuild.pm,v 1.41 2009/07/16 17:43:52 tgl Exp $
 #
 use Carp;
 use Win32;
@@ -44,7 +44,7 @@ sub mkvcbuild
 
     our @pgportfiles = qw(
       chklocale.c crypt.c fseeko.c getrusage.c inet_aton.c random.c srandom.c
-      getaddrinfo.c gettimeofday.c kill.c open.c rand.c
+      getaddrinfo.c gettimeofday.c kill.c open.c erand48.c
       snprintf.c strlcat.c strlcpy.c copydir.c dirmod.c exec.c noblock.c path.c pipe.c
       pgsleep.c pgstrcasecmp.c qsort.c qsort_arg.c sprompt.c thread.c
       getopt.c getopt_long.c dirent.c rint.c win32env.c win32error.c);