Skip to content

std::random_device always returns constant value #8201

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

Closed
1 task done
Zhu-jiatong opened this issue May 13, 2023 · 6 comments
Closed
1 task done

std::random_device always returns constant value #8201

Zhu-jiatong opened this issue May 13, 2023 · 6 comments
Labels
Status: Awaiting triage Issue is waiting for triage

Comments

@Zhu-jiatong
Copy link

Zhu-jiatong commented May 13, 2023

Board

Irrelevant

Device Description

ESP32 NodeMCU

Hardware Configuration

Bare ESP32 Board

Version

v2.0.9

IDE Name

Visual Micro (Arduino IDE)

Operating System

Windows 11

Flash frequency

80Mhz

PSRAM enabled

no

Upload speed

115200

Description

I intended to use a real random number generator for cryptographic purposes. So, I choose std::random_device, which should be a platform independent way that is able to return real random numbers.

However, the generator produces the same value 3499211612 every time the ESP32 is powered on, which is not random.

Sketch

#include <random>
void setup()
{
 Serial.begin(115200);
 ... // wait until Serial is ready;
 std::cout << std::random_device()();
}

Debug Message

Output on serial monitor every time the board is powered on: `3499211612`

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@Zhu-jiatong Zhu-jiatong added the Status: Awaiting triage Issue is waiting for triage label May 13, 2023
@45gfg9
Copy link

45gfg9 commented May 13, 2023

Reproducible for me. Just had a quick look at cppreference and found this:

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

Also note you are creating a fresh std::random_device each time. The title would be a bit of misleading.

But, yes, ESP32 does have hardware RNG so PRNG does not seem like an acceptable behavior. I think this is an STL's fault, not really Arduino SDK's.

@mrengineer7777
Copy link
Collaborator

This behavior may be intentional. Possibly related: #7848. Does the issue occur for an older version, say 2.0.6?

@Zhu-jiatong
Copy link
Author

I haven't been able to test it on older versions yet, as the Visual Micro board manager only provides the option to install the newest version.

@igrr
Copy link
Member

igrr commented May 14, 2023

This is not intentional, we have overlooked checking how this function is implemented in libstdc++. I would recommend opening a feature request in IDF for this, sounds like something we could support.

@Zhu-jiatong
Copy link
Author

Ok, I submitted a feature request in the IDF's repository.

@SuGlider
Copy link
Collaborator

@Zhu-jiatong - std::random_device is not part of Arduino Core implementation, as already explained above.
Therefore, I'll close this issue and let you follow it under IDF's github.

As for Arduino related random functionality, #7848 has an example about how to generate pseudo and real random sequences using Arduino APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage
Projects
None yet
Development

No branches or pull requests

5 participants