Zepp-Hanzj opened a new pull request, #19244:
URL: https://github.com/apache/nuttx/pull/19244
## Summary
The existing first-order LCG (seed = 470001 * seed % 999563) has several
quality problems:
- Output range limited to [1, 999562] (~20 bits) instead of full 32-bit
- Lower bits have very short periods (8-bit period = 1, 16-bit = 105)
- Overall period only ~1M, far too short for many applications
- Causes mbedtls_rsa_gen_key to loop forever when rand() consumption aligns
with the cycle length (#16760)
Replace the entire order-based LCG implementation (CONFIG_LIBC_RAND_ORDER
0-3) with Marsaglia's xorshift32:
- Full 32-bit output range
- Period 2^32 - 1 (~4.29 billion)
- Fast: just three XOR/shift operations
- No floating-point math needed
- No CONFIG_LIBC_RAND_ORDER configuration required
Remove the CONFIG_LIBC_RAND_ORDER Kconfig option and clean up all defconfig
references (12 boards) and related comments.
## Verification
Built and tested on sim:nsh. Replaced hello_main.c with a test program that
checks rand() quality, then restored it after testing.
Build command:
make distclean
tools/configure.sh sim:nsh
make -j$(nproc)
Run command:
printf 'hello\npoweroff\n' | timeout 30 ./nuttx 2>/dev/null
Test output (3 consecutive runs, all identical as expected for deterministic
PRNG with same seed):
NuttShell (NSH) NuttX-13.0.0-RC0
nsh> hello
=== rand() quality test ===
Range after 10000 calls: [84907, 2147393180]
Expected: [0, 2147483647]
Full value repeat within 500k: NOT FOUND (period=0)
Expected: NOT FOUND (period should be ~4 billion)
srand(0) first call: 270369 (should be non-zero)
nrand(100) = 89 (should be 0-99)
=== test complete ===
nsh> poweroff
What each check means:
Range [84907, 2147393180] — max value close to INT_MAX (2147483647), old
LCG only reached ~999562
Full value repeat: NOT FOUND — no repeat within 500k calls, old LCG
repeated within ~1M
srand(0) non-zero — zero seed does not degenerate the generator
nrand(100) in [0,99] — range mapping works correctly
Also confirmed via nm that the NuttX xorshift32 is linked into the binary
(not host GLIBC):
$ nm nuttx | grep xorshift32
000000004019d160 b __gcov0.xorshift32
00000000401740a0 d __gcov_.xorshift32
00000000400c4052 t xorshift32
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]