Implement rand/srand in terms of random/srandom.

Code developed for glibc or older versions of bionic might expect more
randomness than the BSD implementation provides.

Bug: 15829381

(cherry picked from commit 76c241b091b4d9d9a9237d40e814e52ce2917f47)

Change-Id: If721b3f16efdb21cb67df5ec5034c0ba905bd029
diff --git a/libc/Android.mk b/libc/Android.mk
index 1345159..51002e5 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -177,6 +177,7 @@
     bionic/pthread_sigmask.cpp \
     bionic/ptrace.cpp \
     bionic/raise.cpp \
+    bionic/rand.cpp \
     bionic/readlink.cpp \
     bionic/reboot.cpp \
     bionic/recv.cpp \
@@ -294,7 +295,6 @@
     upstream-netbsd/lib/libc/stdlib/mrand48.c \
     upstream-netbsd/lib/libc/stdlib/nrand48.c \
     upstream-netbsd/lib/libc/stdlib/_rand48.c \
-    upstream-netbsd/lib/libc/stdlib/rand.c \
     upstream-netbsd/lib/libc/stdlib/rand_r.c \
     upstream-netbsd/lib/libc/stdlib/seed48.c \
     upstream-netbsd/lib/libc/stdlib/srand48.c \
diff --git a/libc/bionic/rand.cpp b/libc/bionic/rand.cpp
new file mode 100644
index 0000000..0074f2d
--- /dev/null
+++ b/libc/bionic/rand.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+
+// The BSD rand/srand is very weak. glibc just uses random/srandom instead.
+// Since we're likely to run code intended for glibc, and POSIX doesn't seem
+// to disallow this, we go that route too.
+
+int rand() {
+  return random();
+}
+
+void srand(unsigned int seed) {
+  return srandom(seed);
+}
diff --git a/libc/upstream-netbsd/lib/libc/stdlib/rand.c b/libc/upstream-netbsd/lib/libc/stdlib/rand.c
deleted file mode 100644
index 4909d14..0000000
--- a/libc/upstream-netbsd/lib/libc/stdlib/rand.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*	$NetBSD: rand.c,v 1.12 2012/06/25 22:32:45 abs Exp $	*/
-
-/*-
- * Copyright (c) 1990, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)rand.c	8.1 (Berkeley) 6/14/93";
-#else
-__RCSID("$NetBSD: rand.c,v 1.12 2012/06/25 22:32:45 abs Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
-#include <sys/types.h>
-#include <stdlib.h>
-
-static u_long next = 1;
-
-int
-rand(void)
-{
-	/* LINTED integer overflow */
-	return (int)((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
-}
-
-void
-srand(u_int seed)
-{
-	next = seed;
-}
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 7c86d76..6d29421 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -53,17 +53,10 @@
 
 TEST(stdlib, rand) {
   srand(0x01020304);
-#if defined(__BIONIC__)
-  EXPECT_EQ(1675538669, rand());
-  EXPECT_EQ(1678228258, rand());
-  EXPECT_EQ(1352350131, rand());
-  EXPECT_EQ(824068976, rand());
-#else
   EXPECT_EQ(55436735, rand());
   EXPECT_EQ(1399865117, rand());
   EXPECT_EQ(2032643283, rand());
   EXPECT_EQ(571329216, rand());
-#endif
 }
 
 TEST(stdlib, mrand48) {