Skip to content

Commit 9872e59

Browse files
committed
Added two new compile flags, TE_RAND_SEED_TIME and TE_RAND_SEED
TE_RAND_SEED_TIME uses time to see the mersenne twiser (ESP32 microcontrollers don't support entropy devices).
1 parent b72973c commit 9872e59

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

docs/manual/compile-time-options.qmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ Compile with `TE_LONG_DOUBLE` defined to use `long double` as the default data t
1818
Depending on the compiler, this may provide support for handling `uint64_t` values. Call `te_parser::supports_64bit()`
1919
(or `SUPPORTS64BIT()` in an expression at runtime) to confirm this.
2020

21+
## `TE_RAND_SEED` {-}
22+
23+
Define this as an unsigned integer to seed the random number generator with.
24+
This will affect the function `RAND()`, causing it to produce a deterministic series of numbers.
25+
26+
## `TE_RAND_SEED_TIME` {-}
27+
28+
Compile with `TE_RAND_SEED_TIME` to seed the random number generator with the current time.
29+
This is useful if compiling for microcontrollers that do not support entropy devices.
30+
31+
The default is to seed the random number generator with `std::random_device`.
32+
2133
## `TE_BITWISE_OPERATORS` {-#te-bitwise-ops}
2234

2335
By default, the operators `&`, `|`, and `^` represent logical AND, logical OR, and exponentiation. If `TE_BITWISE_OPERATORS` is defined,

docs/manual/embedded-programming.qmd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ case insensitivity, and bookkeeping operations.
2121

2222
Refer to [compile-time options](#compile-time-options) for flags that can provide optimization.
2323

24+
## Device Compatibility
25+
26+
By default, *TinyExpr++* uses `std::random_device` to seed its random number generator, which may cause compatibility issues with some microcontrollers.
27+
To instead seed it with the current time, compile with `TE_RAND_SEED_TIME`.
28+
2429
## Volatility {-}
2530

2631
If needing to use a `te_parser` object as `volatile`

tests/tetests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,8 +2202,11 @@ TEST_CASE("Random", "[random]")
22022202
{
22032203
te_parser tep;
22042204
// can't have reproducible results for rand, so just run it and make
2205-
// sure it doesn't crash
2205+
// sure it doesn't crash...
22062206
CHECK_NOTHROW(tep.evaluate("rand()"));
2207+
// ...and within 0-1
2208+
auto res = tep.evaluate("rand()");
2209+
CHECK((res >= 0 && res <= 1));
22072210
}
22082211

22092212
TEST_CASE("Available functions", "[available]")

tinyexpr.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,15 @@ namespace te_builtins
360360
[[nodiscard]]
361361
static te_type te_random()
362362
{
363-
std::random_device rdev;
363+
#ifdef TE_RAND_SEED
364+
std::mt19937 gen(static_cast<unsigned int>(RAND_SEED));
365+
#elif defined(TE_RAND_SEED_TIME)
366+
std::mt19937 gen(static_cast<unsigned int>(time(nullptr)));
367+
#else
368+
static std::random_device rdev;
364369
std::mt19937 gen(rdev());
370+
#endif
371+
365372
std::uniform_real_distribution<te_type> distr(0, 1);
366373
return distr(gen);
367374
}

tinyexpr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <cstdio>
6060
#include <cstdlib>
6161
#include <cstring>
62+
#include <ctime>
6263
#include <functional>
6364
#include <initializer_list>
6465
#include <limits>
@@ -76,6 +77,10 @@
7677

7778
class te_parser;
7879

80+
#if defined(TE_RAND_SEED) && defined(TE_RAND_SEED_TIME)
81+
#error TE_RAND_SEED and TE_RAND_SEED_TIME compile options cannot be combined. Only one random number generator seeding method can be specified.
82+
#endif
83+
7984
#if defined(TE_FLOAT) && defined(TE_LONG_DOUBLE)
8085
#error TE_FLOAT and TE_LONG_DOUBLE compile options cannot be combined. Only one data type can be specified.
8186
#endif

0 commit comments

Comments
 (0)