<random> library cannot compile with MBED OS

Perhaps this is a known issue, but I cannot find much reference to it.

is part of the standard library of C++.

Whenever I include in any c++ program (even one with an empty setup() and loop()):

#include <random>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

I get an error:

In file included from C:\Users\jmdod\AppData\Local\arduino\sketches\616BE7B5631EB5ABB17A18FC8E74A54A\sketch\sketch_feb6a.ino.cpp:1:0:
c:\users\jmdod\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\bits\random.tcc: In member function 'std::poisson_distribution<_IntType>::result_type std::poisson_distribution<_IntType>::operator()(_UniformRandomNumberGenerator&, const std::poisson_distribution<_IntType>::param_type&)':
C:\Users\jmdod\AppData\Local\Arduino15\packages\arduino\hardware\mbed_giga\4.2.1\cores\arduino/Arduino.h:63:16: error: expected unqualified-id before '(' token
** #define abs(x) ((x)>0?(x):-(x))**
** ^**
C:\Users\jmdod\AppData\Local\Arduino15\packages\arduino\hardware\mbed_giga\4.2.1\cores\arduino/Arduino.h:63:16: error: expected unqualified-id before '(' token
** #define abs(x) ((x)>0?(x):-(x))**
** ^**
c:\users\jmdod\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\bits\random.tcc: In member function 'std::binomial_distribution<_IntType>::result_type std::binomial_distribution<_IntType>::operator()(_UniformRandomNumberGenerator&, const std::binomial_distribution<_IntType>::param_type&)':
C:\Users\jmdod\AppData\Local\Arduino15\packages\arduino\hardware\mbed_giga\4.2.1\cores\arduino/Arduino.h:63:16: error: expected unqualified-id before '(' token
** #define abs(x) ((x)>0?(x):-(x))**
** ^**
C:\Users\jmdod\AppData\Local\Arduino15\packages\arduino\hardware\mbed_giga\4.2.1\cores\arduino/Arduino.h:63:16: error: expected unqualified-id before '(' token
** #define abs(x) ((x)>0?(x):-(x))**
** ^**

exit status 1

Compilation error: exit status 1

I tried both MBED OS parts I use (RP2040 Connect and Giga R1) with the same result.

Anything that includes something that eventually does a #include <cmath> or #include <math.h> is going to likely collide with the macros in Arduino.h. The usual solution to such problems is don't use the C++ standard library function, use the Arduino equivalent. Which in this case, would be Arduino's built in version of random.

Thanks for the answer, which is what I suspected. However, the random() function provided by Arduino is pretty limited. It only returns integers what I assume is a uniform distribution. I'm looking for meatier distributions like normal or poisson. Also, it compiles for ESP32. So, its definitely an MBedOS issue. But, since MBedOS won't be updated anymore, I guess this will not be an option.

Compiles error-free for Teensy 4.1 too.

As each core has its own version of Arduino.h and implement things according to its own needs and wants, it's not a big surprise that some have macros for things, some have included functions, and some expect other libraries. You have to keep in mind that there was and is very limited memory available for some of the smaller (and earlier) platforms and supporting the standard C++ library on them was simply not possible.

Absolutely makes sense. Only the Arduino Giga has more memory, and I was hoping it would be a bit more capable. Its not a criticism. The fact that most of the standard library does work has been extremely useful for me. Can't have everything...or there would be nothing to gripe about while I drink my coffee.

The GIGA has a SE05X secure module that can generate random. Not used it so not sure of its capabilities. ArduinoCore-mbed/libraries/SE05X/examples/SE05XRandomNumber/SE05XRandomNumber.ino at 53a89c3cbd20dae1de6caa466b6f04ed734aaf0d · arduino/ArduinoCore-mbed · GitHub

The MCU also has RNG built in https://www.st.com/content/ccc/resource/training/technical/product_training/group0/5a/ba/43/28/46/d0/44/aa/STM32H7-Security-Random_Number_Generator_RNG/files/STM32H7-Security-Random_Number_Generator_RNG.pdf/_jcr_content/translations/en.STM32H7-Security-Random_Number_Generator_RNG.pdf

The problem is that Arduino.h defines abs as a macro, which can break a lot if things if a class tries to define its own abs function.

Either include random before Arduino.h or undefine abs.

#include <random>
#include <Arduino.h> // explicit include here => not added automatically at the beginning
#undef abs
#include <random>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.