Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random(int32_t min, int32_t max) unexpected results #328

Closed
tshcherban opened this issue Jul 12, 2021 · 2 comments
Closed

random(int32_t min, int32_t max) unexpected results #328

tshcherban opened this issue Jul 12, 2021 · 2 comments
Labels
bug Something isn't working resolved Issue was resolved (e.g. bug fixed, or feature implemented)

Comments

@tshcherban
Copy link

tshcherban commented Jul 12, 2021

Is there a bug in random generate function (overload with min/max parameters)?

To Reproduce

SX1262 radio = new Module(NSS, DIO1, NRST, BUSY);
.
.
.
PhysicalLayer *pl = &radio;
for (int i = 0; i < 20; ++i)
{
  int32_t pause = pl->random(2500, 6500);
  Serial.println(String(pause));
}

Actual result

-783
2900
5732
-545
2526
4675
5544
4978
2538
5929
2069
4129
3148
4257
-3
2002
6389
6400
-942
830

Expected behavior
Should return something between specified parameters.

Additional info (please complete):

  • MCU: Atmega328, ESP32
  • Wireless module SX1262
  • Arduino IDE version 1.8.13
  • Library version 4.4.2
@tshcherban
Copy link
Author

tshcherban commented Jul 12, 2021

If i understood correctly, randNum after raw initialization can contain ANY number in range of int32_t.
I'm not strong in math but that line looks strange:
image
It does clipping the number from top, but not from bottom. Maybe use map or its equivalent formula here?

@jgromes
Copy link
Owner

jgromes commented Jul 18, 2021

The min/max implementation is the same as in Arduino AVR core to achieve the exact same behavior as Arduino random() function. There was however a bug in negative number handling.

Fixed in last commit, thanks for reporting!

@jgromes jgromes closed this as completed Jul 18, 2021
@jgromes jgromes added bug Something isn't working resolved Issue was resolved (e.g. bug fixed, or feature implemented) labels Jul 18, 2021
phretor added a commit to rfquack/RadioLib that referenced this issue Sep 16, 2021
* 'master' of git://github.com/jgromes/RadioLib: (83 commits)
  [nRF24] Added interrupt-driven examples
  [AX.25] Added option to adjust audio frequencies (jgromes#346)
  [MQTT] User SerialModule wrapper
  [HTTP] User SerialModule wrapper
  [XBee] Use SerialModule wrapper
  [JDY08] Use SerialModule wrapper
  [HC05] Use SerialModule wrapper
  Added SerialModule wrapper class (jgromes#305)
  [CC1101] Fixed blocking receive always returning timeout (jgromes#348)
  [CC1101] Added 0x17 as valid version number (jgromes#349)
  Added AFSK via OOK example
  Bump version to 4.5.0
  [Si443x] Fixed rxosr calculation (jgromes#199)
  [Si443x] Added antenna switching on GPIO0/1
  [Si443x] Fixed preamble configuration (jgromes#199)
  [Si443x] Added software reset
  [PHY] Fixed negative random numbers (jgromes#328)
  [RF69] Renamed TRNG method
  [RF69] Renamed TRNG method
  [SX126x] Renamed TRNG method
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resolved Issue was resolved (e.g. bug fixed, or feature implemented)
Projects
None yet
Development

No branches or pull requests

2 participants