commit 5322cbec9cd0fc0212a87e4b24ff73fa28133c94
parent c8f6661a9dab4730d2b921e7edd0b283b9ff209e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 23 Feb 2017 19:25:08 +0100
[libc] Add rand()
Diffstat:
6 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/libc/include/bits/amd64-sysv/arch/stdlib.h b/libc/include/bits/amd64-sysv/arch/stdlib.h
@@ -11,7 +11,6 @@ typedef unsigned long size_t;
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
diff --git a/libc/include/bits/i386-sysv/arch/stdlib.h b/libc/include/bits/i386-sysv/arch/stdlib.h
@@ -11,7 +11,6 @@ typedef unsigned long size_t;
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
diff --git a/libc/include/bits/qbe/arch/stdlib.h b/libc/include/bits/qbe/arch/stdlib.h
@@ -11,7 +11,6 @@ typedef unsigned long size_t;
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef int wchar_t;
diff --git a/libc/include/bits/z80/arch/stdlib.h b/libc/include/bits/z80/arch/stdlib.h
@@ -11,7 +11,6 @@ typedef unsigned size_t;
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define RAND_MAX 32767
#ifndef _WCHAR_T
typedef long wchar_t;
diff --git 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;
diff --git a/libc/src/rand.c b/libc/src/rand.c
@@ -0,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;
+}