2012-12-01 16:32:03 +00:00
|
|
|
/****************************************************************************
|
2018-05-29 13:21:26 -06:00
|
|
|
* libs/libc/stdlib/lib_srand.c
|
2012-11-10 16:06:01 +00:00
|
|
|
*
|
2024-09-25 14:05:00 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
|
* ASF licenses this file to you 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
|
2012-11-10 16:06:01 +00:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 16:06:01 +00:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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.
|
2012-11-10 16:06:01 +00:00
|
|
|
*
|
2012-12-01 16:32:03 +00:00
|
|
|
****************************************************************************/
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2012-12-01 16:32:03 +00:00
|
|
|
/****************************************************************************
|
2012-11-10 16:06:01 +00:00
|
|
|
* Included Files
|
2012-12-01 16:32:03 +00:00
|
|
|
****************************************************************************/
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2016-07-17 07:56:25 -06:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2012-11-10 16:06:01 +00:00
|
|
|
#include <stdlib.h>
|
2026-07-01 11:45:15 +08:00
|
|
|
#include <stdint.h>
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2016-07-21 14:05:44 -06:00
|
|
|
#include <nuttx/lib/lib.h>
|
2016-07-17 07:56:25 -06:00
|
|
|
|
2012-12-01 16:32:03 +00:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Data
|
|
|
|
|
****************************************************************************/
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
static uint32_t g_randstate = 1;
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2012-12-01 16:32:03 +00:00
|
|
|
/****************************************************************************
|
2012-11-10 16:06:01 +00:00
|
|
|
* Private Functions
|
2012-12-01 16:32:03 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: xorshift32
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Marsaglia's xorshift32 PRNG. Period 2^32 - 1 (about 4.29 billion).
|
|
|
|
|
* Much better statistical properties than a simple LCG with a small
|
|
|
|
|
* modulus, while remaining fast (three XOR/shift operations).
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
static inline uint32_t xorshift32(FAR uint32_t *state)
|
2012-11-10 16:06:01 +00:00
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
uint32_t x = *state;
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
/* State must never be zero, otherwise the generator is stuck.
|
|
|
|
|
* The seed path (srand / rand_r) already guards against this, but we
|
|
|
|
|
* check here as well for safety.
|
2012-12-02 17:34:08 +00:00
|
|
|
*/
|
|
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
if (x == 0)
|
2012-12-02 17:34:08 +00:00
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
x = 1;
|
2012-12-02 17:34:08 +00:00
|
|
|
}
|
2012-12-07 16:00:56 +00:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
x ^= x << 13;
|
|
|
|
|
x ^= x >> 17;
|
|
|
|
|
x ^= x << 5;
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
*state = x;
|
|
|
|
|
return x;
|
2012-12-01 16:32:03 +00:00
|
|
|
}
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2023-06-28 12:29:03 +08:00
|
|
|
static unsigned long nrand_r(unsigned long limit,
|
2026-07-01 11:45:15 +08:00
|
|
|
FAR uint32_t *state)
|
2023-06-28 12:29:03 +08:00
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
uint32_t randval = xorshift32(state);
|
2023-06-28 12:29:03 +08:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
/* Map the 32-bit random value into the range [0, limit).
|
|
|
|
|
* The modulo bias is negligible for small limits relative to 2^32.
|
|
|
|
|
*/
|
2023-06-28 12:29:03 +08:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
return (unsigned long)(randval % (uint32_t)limit);
|
2023-06-28 12:29:03 +08:00
|
|
|
}
|
|
|
|
|
|
2012-12-01 16:32:03 +00:00
|
|
|
/****************************************************************************
|
2012-11-10 16:06:01 +00:00
|
|
|
* Public Functions
|
2012-12-01 16:32:03 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-17 07:56:25 -06:00
|
|
|
* Name: srand
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2026-07-01 11:45:15 +08:00
|
|
|
* Seed the random number generator.
|
2016-07-17 07:56:25 -06:00
|
|
|
*
|
2012-12-01 16:32:03 +00:00
|
|
|
****************************************************************************/
|
2012-11-10 16:06:01 +00:00
|
|
|
|
|
|
|
|
void srand(unsigned int seed)
|
|
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
/* Avoid a zero state which would make xorshift32 degenerate. */
|
|
|
|
|
|
|
|
|
|
g_randstate = (seed == 0) ? 1 : (uint32_t)seed;
|
2012-12-01 16:32:03 +00:00
|
|
|
}
|
2012-11-10 16:06:01 +00:00
|
|
|
|
2016-07-17 07:56:25 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrand
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2016-07-17 08:31:02 -06:00
|
|
|
* Return a random, unsigned long value in the range of 0 to (limit - 1)
|
2016-07-17 07:56:25 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned long nrand(unsigned long limit)
|
2012-11-10 16:06:01 +00:00
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
return nrand_r(limit, &g_randstate);
|
2023-06-28 12:29:03 +08:00
|
|
|
}
|
2016-07-17 07:56:25 -06:00
|
|
|
|
2023-06-28 12:29:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: rand_r
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* The function rand() is not reentrant, since it uses hidden state that
|
|
|
|
|
* is modified on each call. This might just be the seed value to be used
|
|
|
|
|
* by the next call, or it might be something more elaborate. In order to
|
|
|
|
|
* get reproducible behavior in a threaded application, this state must be
|
|
|
|
|
* made explicit; this can be done using the reentrant function rand_r().
|
|
|
|
|
*
|
|
|
|
|
* Return a random, int value in the range of 0 to INT_MAX.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2016-07-17 08:31:02 -06:00
|
|
|
|
2023-06-28 12:29:03 +08:00
|
|
|
int rand_r(FAR unsigned int *seedp)
|
|
|
|
|
{
|
2026-07-01 11:45:15 +08:00
|
|
|
uint32_t state = (uint32_t)*seedp;
|
2023-06-28 12:29:03 +08:00
|
|
|
unsigned long rand;
|
2016-07-17 07:56:25 -06:00
|
|
|
|
2026-07-01 11:45:15 +08:00
|
|
|
if (state == 0)
|
|
|
|
|
{
|
|
|
|
|
state = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rand = nrand_r(INT_MAX, &state);
|
|
|
|
|
*seedp = (unsigned int)state;
|
2023-06-28 12:29:03 +08:00
|
|
|
return (int)rand;
|
2012-12-01 16:32:03 +00:00
|
|
|
}
|