File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed
Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change 1111#pragma once
1212
1313#include < WString.h>
14+ #include < limits>
1415#include < esp_systemapi.h>
1516
1617/* *
@@ -106,11 +107,14 @@ template <typename T> struct TRange {
106107 */
107108 T random () const
108109 {
109- auto n = 1 + max - min;
110+ uint64_t n = 1 + max - min;
110111 if (n == 0 ) {
111112 return 0 ;
112113 }
113- auto value = os_random ();
114+ T value = os_random ();
115+ if (n > std::numeric_limits<uint32_t >::max ()) {
116+ value |= uint64_t (os_random ()) << 32 ;
117+ }
114118 return min + value % n;
115119 }
116120
Original file line number Diff line number Diff line change @@ -19,6 +19,10 @@ class RangeTest : public TestGroup
1919
2020 constexpr int64_t tmp = 0x8000000000LL ;
2121 static_assert (!range.contains (tmp));
22+
23+ for (unsigned i = 0 ; i < 10 ; ++i) {
24+ Serial << range.random () << endl;
25+ }
2226 }
2327
2428 TEST_CASE (" truncation" )
@@ -35,6 +39,14 @@ class RangeTest : public TestGroup
3539 int val = 0x8000 ;
3640 REQUIRE (!range.contains (val));
3741 }
42+
43+ TEST_CASE (" Random" )
44+ {
45+ constexpr TRange<int64_t > range (-0x10000000000LL , 0x10000000000LL );
46+ for (unsigned i = 0 ; i < 10 ; ++i) {
47+ Serial << range.random () << endl;
48+ }
49+ }
3850 }
3951};
4052
You can’t perform that action at this time.
0 commit comments