1
0
Fork 0
forked from nuttx/nuttx-update

libs/libc/stdlib and include/stdlib.h: Add implementation of random() and srandom().

This commit is contained in:
Gregory Nutt 2018-07-19 11:21:49 -06:00
parent ac5618239c
commit 58bbb66481
2 changed files with 24 additions and 5 deletions

View file

@ -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 <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -45,6 +45,7 @@
#include <sys/types.h>
#include <stdint.h>
#include <limits.h>
/****************************************************************************
* 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 */

View file

@ -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 <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -41,6 +41,7 @@
#include <sys/types.h>
#include <stdlib.h>
#include <limits.h>
#include <nuttx/lib/lib.h>
@ -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);
}