| The solution isn't to stop using rand(). The solution is to stop using newlib. If you're doing your own custom memory management like this, you shouldn't even have a malloc implementation at all. Even newlib is too bloated for your use case. At this point, chances are you're using a trivial subset of the C library and it'd be easy to roll your own. You can import bits and pieces from other projects (I personally sometimes copy and paste bits from PDClib for this). In such a tight embedded project, chances are you don't even have threads; why even pull in that reentrancy code? Freestanding C code with no standard library isn't scary. If you need an example, look at what we do for m1n1: https://github.com/AsahiLinux/m1n1/tree/main/src In particular, this is the libc subset we use (we do have malloc here, which is dlmalloc, but still not enough of libc to be worth depending on a full one): https://github.com/AsahiLinux/m1n1/tree/main/sysinc |
It’s a trap that people fall into to think that it’s easy to roll your own. It depends on what, exactly, you are rolling, what resources you have, etc. Maybe I need a couple math functions, a decent implementation of memset/memcpy/etc, and having the C library at my disposal gives me those things.
The idea that you throw out Newlib just because one function pulled in malloc seems unjustifiable to me.
On the other hand, I think it’s quite normal to want your own PRNG.