ref: ba40e25f97d3e1666ec3a88c9a536c7876436fba
parent: d94045063ac8a93222b4f08ac56c056a7a6a2415
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Feb 23 14:25:08 EST 2017
[libc] Add rand()
--- a/libc/include/bits/amd64-sysv/arch/stdlib.h
+++ b/libc/include/bits/amd64-sysv/arch/stdlib.h
@@ -11,7 +11,6 @@
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
--- a/libc/include/bits/i386-sysv/arch/stdlib.h
+++ b/libc/include/bits/i386-sysv/arch/stdlib.h
@@ -11,7 +11,6 @@
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
--- a/libc/include/bits/qbe/arch/stdlib.h
+++ b/libc/include/bits/qbe/arch/stdlib.h
@@ -11,7 +11,6 @@
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
--- a/libc/include/bits/z80/arch/stdlib.h
+++ b/libc/include/bits/z80/arch/stdlib.h
@@ -11,7 +11,6 @@
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef long wchar_t;
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -5,6 +5,7 @@
#include <arch/stdlib.h>
#define MB_CUR_MAX 1
+#define RAND_MAX 32767
typedef struct {
int quot, rem;
--- /dev/null
+++ b/libc/src/rand.c
@@ -1,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdlib.h>
+
+static int next;
+
+void
+srand(unsigned seed)
+{
+ next = seed;
+}
+
+int
+rand(void) /* RAND_MAX assumed to be 32767. */
+{
+ next = next * 1103515245 + 12345;
+ return (unsigned)(next/65536) % 32768;
+}