File tree 2 files changed +26
-4
lines changed
Filter options
2 files changed +26
-4
lines changed
Original file line number Diff line number Diff line change @@ -137,9 +137,19 @@ typedef unsigned int word;
137
137
void setup (void );
138
138
void loop (void );
139
139
140
+ // The default is using Real Hardware random number generator
141
+ // But when randomSeed() is called, it turns to Psedo random
142
+ // generator, exactly as done in Arduino mainstream
143
+ long random (long );
140
144
long random (long , long );
141
- #endif
145
+ // Calling randomSeed() will make random()
146
+ // using pseudo random like in Arduino
142
147
void randomSeed (unsigned long );
148
+ // Allow the Application to decide if the random generator
149
+ // will use Real Hardware random generation (true - default)
150
+ // or Pseudo random generation (false) as in Arduino MainStream
151
+ void useRealRandomGenerator (bool useRandomHW);
152
+ #endif
143
153
long map (long , long , long , long , long );
144
154
145
155
#ifdef __cplusplus
@@ -207,8 +217,6 @@ void setToneChannel(uint8_t channel = 0);
207
217
void tone (uint8_t _pin, unsigned int frequency, unsigned long duration = 0 );
208
218
void noTone (uint8_t _pin);
209
219
210
- // WMath prototypes
211
- long random (long );
212
220
#endif /* __cplusplus */
213
221
214
222
#include " pins_arduino.h"
Original file line number Diff line number Diff line change @@ -29,10 +29,22 @@ extern "C" {
29
29
}
30
30
#include " esp32-hal-log.h"
31
31
32
+ // Allows the user to choose between Real Hardware
33
+ // or Software Pseudo random generators for the
34
+ // Arduino random() functions
35
+ static bool s_useRandomHW = true ;
36
+ void useRealRandomGenerator (bool useRandomHW) {
37
+ s_useRandomHW = useRandomHW;
38
+ }
39
+
40
+ // Calling randomSeed() will force the
41
+ // Pseudo Random generator like in
42
+ // Arduino mainstream API
32
43
void randomSeed (unsigned long seed)
33
44
{
34
45
if (seed != 0 ) {
35
46
srand (seed);
47
+ s_useRandomHW = false ;
36
48
}
37
49
}
38
50
@@ -46,7 +58,9 @@ long random( long howbig )
46
58
if (howbig < 0 ) {
47
59
return (random (0 , -howbig));
48
60
}
49
- return esp_random () % howbig;
61
+ // if randomSeed was called, fall back to software PRNG
62
+ uint32_t val = (s_useRandomHW) ? esp_random () : rand ();
63
+ return val % howbig;
50
64
}
51
65
52
66
long random (long howsmall, long howbig)
You can’t perform that action at this time.
0 commit comments