zephyr/include/sys
Yasushi SHOJI b2fde24c4c libc: minimal: Add rand() and srand()
rand() and srand() are pseudo-random number generator functions
defined in ISO C. This implementation uses the Linear Congruential
Generator (LCG) algorithm with the following parameters, which are the
same as used in GNU Libc "TYPE_0" algorithm.

  Modulus 2^31
  Multiplier 1103515245
  Increment 12345
  Output Bits 30..0

Note that the default algorithm used by GNU Libc is not TYPE_0, and
TYPE_0 should be selected first by an initstate() call as shown below.

All global variables in a C library must be routed to a memory
partition in order to be used by user-mode applications when
CONFIG_USERSPACE is enabled.  Thus, srand_seed is marked as
such. z_libc_partition is originally used by the Newlib C library but
it's generic enough to be used by either the minimal libc or the
newlib.

All other functions in the Minimal C library, however, don't require
global variables/states.  Unconditionally using z_libc_partition with
the minimal libc might be a problem for applications utilizing many
custom memory partitions on platforms with a limited number of MPU
regions (eg. Cortex M0/M3). This commit introduces a kconfig option
CONFIG_MINIMAL_LIBC_RAND so that applications can enable the
functions if needed.  The option is disabled by default.

Because this commit _does_ implement rand() and srand(), our coding
guideline check on GitHub Action finds it as a violation.

    Error: lib/libc/minimal/include/stdlib.h:45:WARNING: Violation to
    rule 21.2 (Should not used a reserved identifier) - srand

But this is false positive.

The following is a simple test program for LCG with GNU Libc.

  #include <stdio.h>
  #include <stdlib.h>

  int main()
  {
          static char state[8];

          /* Switch GLIBC to use LCG/TYPE_0 generator type. */
          initstate(0, state, sizeof(state));

          srand(1);  /* Or any other value. */
          printf("%d\n", rand());
          printf("%d\n", rand());

          return 0;
  }

See initstate(3p) for more detail about how to use LCG in GLIBC.

Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
2021-07-20 13:32:36 -04:00
..
__assert.h
arch_interface.h sys: arch_interface: fix doxygen groups 2021-05-21 12:17:04 -04:00
atomic_arch.h kernel: atomic: possibility of arch specific implementation 2021-05-03 11:57:46 +02:00
atomic_builtin.h kernel: atomic: consistently use named type for atomic pointer values 2021-04-19 15:22:13 +02:00
atomic_c.h kernel: atomic: consistently use named type for atomic pointer values 2021-04-19 15:22:13 +02:00
atomic.h kernel: atomic: possibility of arch specific implementation 2021-05-03 11:57:46 +02:00
base64.h
bitarray.h sys: introduce bit arrays 2021-05-07 13:36:22 -04:00
byteorder.h
cbprintf_cxx.h lib: os: cbprintf: Add ARG_UNUSED to unused arguments 2021-05-20 08:23:31 -05:00
cbprintf_internal.h lib: os: cbprintf: Add missing initialization of structure fields 2021-05-20 08:23:31 -05:00
cbprintf.h doc: doxygen: replace option alias with kconfig 2021-06-29 10:26:28 -04:00
check.h
crc.h lib: os: Introduce support for CRC32C algorithm 2021-03-02 14:08:30 +01:00
device_mmio.h devices: mark device MMIO declarations to boot/pinned sections 2021-05-10 16:00:43 -05:00
dlist.h doc: strcutures: use doxygen to reference functions 2021-04-15 14:04:05 -04:00
errno_private.h kernel: support using thread local storage for errno 2020-10-24 10:52:00 -07:00
fdtable.h net: socket: Add locking to prevent concurrent access 2021-04-15 07:16:51 -05:00
kobject.h kernel: change metadata for priv_stacks to const 2021-04-27 13:32:00 -04:00
libc-hooks.h libc: minimal: Add rand() and srand() 2021-07-20 13:32:36 -04:00
list_gen.h
math_extras_impl.h
math_extras.h
mem_manage.h docs: Fix doxygen errors in include/sys/mem_manage.h 2021-05-28 11:30:51 -05:00
mpsc_packet.h lib: os: Add mpsc (multiple producer, single consumer) packet buffer 2021-04-19 10:59:23 -04:00
mpsc_pbuf.h tests: lib: Add test for mpsc_pbuf 2021-04-19 10:59:23 -04:00
mutex.h docs: Add missing retval for sys_mutex_unlock() 2021-03-10 05:42:06 -05:00
notify.h doc: doxygen: replace option alias with kconfig 2021-06-29 10:26:28 -04:00
onoff.h code-guideline: Fixing code violation 10.4 Rule 2020-10-01 17:13:29 -04:00
p4wq.h sys: p4wq: add missing docstring to K_P4WQ_ARRAY_DEFINE 2021-05-21 12:17:04 -04:00
printk.h sys: printk: Fix LOG2 printk support in cpp code 2021-06-29 11:33:21 -04:00
rb.h lib: rb: Use types that indicate size and signedness 2021-04-20 15:50:49 -04:00
reboot.h power: move reboot functionality to os lib 2021-04-28 20:34:00 -04:00
ring_buffer.h doc: group data structure docs and add doxygen linkage 2021-04-15 14:04:05 -04:00
sem.h
sflist.h doc: group data structure docs and add doxygen linkage 2021-04-15 14:04:05 -04:00
slist.h doc: group data structure docs and add doxygen linkage 2021-04-15 14:04:05 -04:00
speculation.h
sys_heap.h kernel/arch: cleanup function definitions 2021-04-01 05:34:17 -04:00
sys_io.h sys_io: Expand the API to 64bits read/write functions 2020-12-08 09:29:20 -05:00
time_units.h include: sys: cast to the same size composite expression 2021-06-24 21:08:56 -04:00
timeutil.h doc: add a section for timeutil APIs 2021-01-20 16:38:56 -05:00
util_internal.h sys: util: Update implementation of loop macros 2021-02-28 16:46:44 -05:00
util_loops.h sys: util: Update implementation of loop macros 2021-02-28 16:46:44 -05:00
util_macro.h sys: util: remove deprecated GET_ARG{1,2} and GET_ARGS_LESS_1 macros 2021-06-11 16:35:19 -05:00
util.h doc: fix doxygen grouping 2021-05-17 11:51:44 -04:00