How to generate the same random twince in ESP32?

Hello. :wave:

I used a NodeMCU V3 before but because of some limitations related to analog inputs, I had to switch to any of ESP32 series.

Now I have a ESP32-WROOM-32E.

In my context, I want to optimize memory writting by using random numbers to adress the values. It before worked like this:

\\This is a pseudo code, just to give an idea
EEPROM.begin(500);
int seed = analogRead(0);
randomSeed(seed);

for(int i=0;i<10;i++){
int value = random(0,500+1);
EEPROM.write(value,i); \\this writes the value i in a random adress
\\EEPROM.commit();
Serial.println(value);
}

randomSeed(seed);
for(int i=0;i<10;i++){
Serial.println(EEPROM.read(random(0,500+1)));
}

The output would be the series of 10 random numbers twince. (If you ignore the chances of rewritting 'i' in the same adress)

I want this same thing in my new board, but it doesn't work.

Code I used to test:

void setup(){
  Serial.begin(115200);
  int semente = analogRead(4);
  delay(3000);
  randomSeed(semente);
  for(int i =0; i<10; i++){
    Serial.println(random(5,15));
  }
  Serial.println("\n");
  randomSeed(semente);
  for(int i =0; i<10; i++){
    Serial.println(random(5,15));
  }

}

void loop(){

}

Output:


Anyone got an idea of how can I achieve that behaviour (of 'reseting' the generated randoms) in my new board? Thanks

Did You check Arduino/reference/random?

1 Like

Sounds like a list of numbers to me.

Yes.

The second paragraph of "Notes and Warnings" isn't working in my ESP32 board. That's my problem.

ESP32, maybe take a look at the Preferences lib..
arduino-esp32/tree/master/libraries/Preferences
Works really good..

good luck.. ~q

Thank you, because I don't know what to look for.

You must save this value in order to use it again to generate a set of random numbers!

but-

Use srand() and rand() because on the esp32, they chose to use hardware randomness and ignore randomSeed().

1 Like

Serial,Print() it on your connected PC and reenter it next run.

Alright, seems a good answer. I will try it and tell if worked.

Thanks already

Remove the int from this line. Because of that int, you created a new local variable in setup(), which ceases to exist, and it's value lost, when setup() ends.

In loop() your code is using a global variable also called semente, which doesn't get assigned a value by any of the code you posted, so is probably zero. When that is given to seed(), random() produces a different sequence

1 Like

What's the reason for doing this at random addresses? It seems like it would make more sense to start at one end and keep moving the address up by the size of the data until you get to the end and start over. That way the wear is levelled across all the cells and it's easy to figure out where the data is.

1 Like

Thank you, it worked.

1 Like

In my application, I save the seed in one of the first adress of the flash memory (aka pseudo EEPROM).

It would definetively work.

However, by doing that, the first adresses will be written way more than the furthest adresses, and you know the flash memory have a limited writting..

You can say "ok but yet still the adress where you save the random seed will be written in a high rate too" and you're right. I have ideas to fix that.

Why? If you keep up with where you're writing there's no reason to always start at the beginning.

Whatever you're planning to do with your random seed would be easier to keep up with just an address.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.