stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.
MIT License
1.35k stars 73 forks source link

crand and crand_u64 generating same number multiple time consecutively #91

Closed Omegaki113r closed 2 months ago

Omegaki113r commented 5 months ago

Hi, First of all thanks for this amazing library.

I mainly work on embedded side of things specially on ESP32 MCUs (Xtensa architecture). I needed to generate 64-bit random numbers therefore i started using crandom api for that.

But for some reason it generates the same number if i try to generate random numbers.

i tried both methods.

1) csrand(time(NULL)) then crand() to generate numbers 2) crand_t r = crand_init(time(NULL)) then crand_u64(&r)

both methods behave in the same way. Not sure this is something coming from the bug of ESPIDF toolchain and compiler or from the STC crandom API as well.

any suggestions or insight about what could be wrong here?

tylov commented 2 months ago

csrand() is only to be set once or when you need to "reset" your sequence. Remember that time(0) only changes once per second too. Note that v50dev (V5.0) will have a difference random number api.