Why does rand() or random() return 0 in threads but is OK in main?

Check the Cube docs or your cross compiler install folder. Supposed it’s a GNU/GCC toolchain there should be a readme file and various license disclaimers. Usually embedded GCC toolchains come with newlib, which is a embedded variant of the standard GNU libc.
Also you should check the map file (might be a build option to generate it along with the elf target) where the rand etc. functions come from.