Skip to content

Adds pseudo random numbers generation #7848

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

Merged
merged 2 commits into from
Feb 15, 2023
Merged

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Feb 14, 2023

Description of Change

Currently radom() uses a real hardware generator.
Arduino mainstream random() uses a software pseudo generator that always returns the same pseudo random sequence based on a seed defined with randomSeed()

This PR implements a similar behavior as the ESP8266 Arduino:

  1. By default, random() will use the ESP32 Hardware real random generator
  2. If the Sketch calls randomSeed(), the Arduino Core will understand that it wants to follow Arduino behavior and it will generate a Pseudo Random number based on that seed.
  3. Adds a new function void useRealRandomGenerator(bool useRandomHW) that, at any time, tells Arduino if random() shall use Hardware or Software random generator.

Tests scenarios

Tested on ESP32

testing sketch:

void printRandomSequence(int val1, int val2) {
//  Serial.printf("random(%d, %d) ::\n", val1, val2);
  Serial.print(random(val1, val2));
  for (uint8_t i = 0; i < 9; i++) {
    Serial.printf(" | %d", random(val1, val2));
  }
  Serial.println();
  
}

void setup() {
  Serial.begin(115200);
  delay(1000); // time for opening the Serial Monitor
  
  Serial.println("\n======\nRandom Testing with random(1, 5)\n======");
  
  Serial.println("\nDefault Hardware Random sequence generation 2x (different seq.)");
  printRandomSequence(1, 5);
  printRandomSequence(1, 5);
  
  Serial.println("\nPseudo Random sequence generation - Seed(100) 2x (same seq.)");
  randomSeed(100); 
  printRandomSequence(1, 5);
  randomSeed(100); 
  printRandomSequence(1, 5);
  
  Serial.println("\nuseRealRandomGenerator(true):: random sequence generation 2x (different seq.)");
  useRealRandomGenerator(true); 
  printRandomSequence(1, 5);
  printRandomSequence(1, 5);

  Serial.println("\nuseRealRandomGenerator(false):: random sequence generation 2x (same seq. - after reboot)");
  useRealRandomGenerator(false); 
  printRandomSequence(1, 5);
  printRandomSequence(1, 5);

  delay(5000);
  Serial.println("\n---Compare the sequences through rebooting...\n");
  esp_restart();
}

void loop() {
// nothing here.  
}

Related links

Fix #7404

@SuGlider SuGlider added this to the 2.0.7 milestone Feb 14, 2023
@SuGlider SuGlider self-assigned this Feb 14, 2023
@SuGlider SuGlider requested a review from me-no-dev February 14, 2023 22:56
@SuGlider SuGlider requested a review from P-R-O-C-H-Y February 14, 2023 23:07
@me-no-dev me-no-dev merged commit 1e3717b into espressif:master Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Make random() and randomSeed() follow Arduino specification
2 participants