From 58bbb664811aa921f44fc9af01ec5b7a60294b42 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 19 Jul 2018 11:21:49 -0600 Subject: [PATCH] libs/libc/stdlib and include/stdlib.h: Add implementation of random() and srandom(). --- include/stdlib.h | 10 +++++++--- libs/libc/stdlib/lib_rand.c | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index 0229bed72f..f8cf9d7b24 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -1,7 +1,7 @@ /**************************************************************************** * include/stdlib.h * - * Copyright (C) 2007-2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2016, 2018 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,6 +45,7 @@ #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -62,9 +63,9 @@ * in sys/types.h. */ -/* Maximum value returned by rand() */ +/* Maximum value returned by rand(). Must be a minimum of 32767. */ -#define RAND_MAX 32767 +#define RAND_MAX INT_MAX /* Integer expression whose value is the maximum number of bytes in a * character specified by the current locale. @@ -144,6 +145,9 @@ extern "C" void srand(unsigned int seed); int rand(void); +#define srandom(s) srand(s) +long random(void); + #ifndef CONFIG_DISABLE_ENVIRON /* Environment variable support */ diff --git a/libs/libc/stdlib/lib_rand.c b/libs/libc/stdlib/lib_rand.c index 1061d33923..529d664535 100644 --- a/libs/libc/stdlib/lib_rand.c +++ b/libs/libc/stdlib/lib_rand.c @@ -1,7 +1,7 @@ /**************************************************************************** * libs/libc/stdlib/lib_rand.c * - * Copyright (C) 2007, 2011, 2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2011, 2016, 2018 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -41,6 +41,7 @@ #include #include +#include #include @@ -59,5 +60,19 @@ int rand(void) { - return (int)nrand(32768L); + return (int)nrand(INT_MAX); +} + +/**************************************************************************** + * Name: random + * + * Description: + * Generate a non-negative, integer random number in the range of 0 through + * (LONG_MAX - 1) + * + ****************************************************************************/ + +long random(void) +{ + return (long)nrand(LONG_MAX); }