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

**URL:** <https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065>\
**Category:** Kernel\
**Created:** [June 18, 2020, 10:32am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065 "2020-06-18T10:32:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 18, 2020, 10:32am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/1 "2020-06-18T10:32:28Z")

</div>

I have an STM32G071 running FreeRTOS CMSIS v2 compiled with STM32CubeIDE. In one of the threads I want to generate a pseudo-random number. (I don’t need anything sophisticated and the STM32G071 doesn’t have an RNG anyway; pseudo-random is absolutely fine for my purposes).

If I call rand() or random() from anywhere in main() before I call osKernelStart(), all is well but if I call rand() or random() from anywhere within a thread they return 0.

**Update** :

I can call rand() or random() in a thread **if I call srand(), srandom(), rand() or random() from main before I start the kernel.**

As far as I can tell, it doesn’t matter which function so long as something calls one of those functions before attempting to call rand() or random() from a thread.

Why would I have to do that?

---

<div class="post-metadata">

**Author:** ![Pilot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pilot](https://forums.freertos.org/u/Pilot)\
**Post date:** [June 18, 2020, 11:16am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/2 "2020-06-18T11:16:39Z")

</div>

Are you sure you’re **really** calling the same functions? It sort of sounds like the wrong rand/random functions are being linked in. I’m not familiar with STM32CubeIDE, but I’ve seen plenty of badly behaved IDEs that will grab unexpected functions from libraries all over the place.

Calling it first in main may create the prototype and then the linker doesn’t grab some far off rand() function and glue it in.

The easiest way to confirm is to compile with no optimization, debug and single step into the rand() function.

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 18, 2020, 11:43am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/3 "2020-06-18T11:43:35Z")

</div>

@Pilot It’s hard to be categorical because you can’t step into them even in the disassembler to the address but given the same call works in one context and not another, I think it’s likely they are the same.

I’m just assuming there is something that rand() or random() depend on that can no longer be accessed once the kernel is running (e.g. the pseudo-random generator doesn’t bother initialising something until it’s called the first time).

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 18, 2020, 11:50am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/4 "2020-06-18T11:50:50Z")

</div>

@Pilot - actually they are the same, I made a copy of the function pointers (which were identical) and called them via that instead.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 18, 2020, 3:33pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/5 "2020-06-18T15:33:41Z")

</div>

Where are the random functions coming from? I assume your C library, and if so, which C library?

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 19, 2020, 3:03pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/6 "2020-06-19T15:03:17Z")

</div>

@rtel Yes, the C library linked in as standard with gcc. I haven’t linked to anything else.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 19, 2020, 5:03pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/7 "2020-06-19T17:03:35Z")

</div>

…and which C library is it? For example, is it newlib?

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 19, 2020, 7:59pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/8 "2020-06-19T19:59:27Z")

</div>

@rtel I’m ashamed to say I don’t know. How do I find out? (I can’t find any mention of newlib in the build settings or the build console output).

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 19, 2020, 9:33pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/9 "2020-06-19T21:33:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [June 19, 2020, 10:57pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/10 "2020-06-19T22:57:19Z")

</div>

CubeIDE does use newlib. There are a couple of problems with the code and projects generated by CubeIDE when using FreeRTOS. See here: [http://www.nadler.com/embedded/newlibAndFreeRTOS.html](http://www.nadler.com/embedded/newlibAndFreeRTOS.html).

Does your generated project have a file called sysmem.c in it? If so, delete it. That version of `_sbrk()` that isn’t compatible with FreeRTOS. It causes `malloc()` (called by newlib code) to fail once the scheduler starts. If you delete sysmem.c, the newlib build included with CubeIDE actually provides a working \_sbrk() for you.

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 22, 2020, 4:50pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/11 "2020-06-22T16:50:32Z")

</div>

@hs2 It appears to be libc\_nano in the map file. Does that make sense?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 22, 2020, 5:22pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/12 "2020-06-22T17:22:29Z")

</div>

Should be ok. Tried it myself also using newlib nano and for me rand() is working when called from a task… strange.

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 23, 2020, 10:19am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/13 "2020-06-23T10:19:40Z")

</div>

@hs2 Yes, very strange.

Oh wait - it looks as though deleting sysmem.c might be fixing it. I found that sometimes, Cube was recreating it but I’m not sure what triggers that. It’s not every time I save the .ioc file.

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [June 23, 2020, 1:09pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/14 "2020-06-23T13:09:16Z")

</div>

@jefftenney Thank you. Deleting sysmem.c seems to make rand() and random() work in tasks/threads although sometimes CubeIDE recreates the file. I’m not sure what causes this - I tried using the CubeIDE editor to change bits of the .ioc file but it didn’t cause sysmem.c to reappear…

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [June 23, 2020, 3:35pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/15 "2020-06-23T15:35:09Z")

</div>

I just ran a quick test with a new test project, and CubeIDE didn’t regenerate sysmem.c for me. I wonder what makes it regenerate that file.

Anyway maybe you could exclude the file from the build then. Right click the file, choose Resource Configurations, Exclude from build, and then exclude it from all build configs.

If that works and if you continue with the library version of `_sbrk()` (not the sysmem.c version), beware that it does not check for the “out-of-memory” condition. That’s usually OK in CubeIDE generated projects because there is generally enough heap memory to satisfy newlib. However, a more robust solution is available at the link I posted before, along with other useful information about using FreeRTOS with newlib.

---

<div class="post-metadata">

**Author:** ![Magicbean](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Magicbean](https://forums.freertos.org/u/Magicbean)\
**Post date:** [July 6, 2020, 9:39am UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/16 "2020-07-06T09:39:26Z")

</div>

@jefftenney You are correct, I got myself confused because I have several projects in my workspace and I was looking at the wrong one. ‘sysmem.c’ is not recreated when generating code from the .ioc file. Sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [July 6, 2020, 2:09pm UTC](https://forums.freertos.org/t/why-does-rand-or-random-return-0-in-threads-but-is-ok-in-main/10065/17 "2020-07-06T14:09:52Z")

</div>

Oh that’s good - thanks for reporting back.
