OK, thanks.
One point that needs emphasis is that the first line must be excluded if it appears to remain from a previous run, or is any way corrupted.
I ran the following code which despite having no monitor activity could not be copied to the clipboard after a Ctrl+A. Reselecting all but that first line restored normal copying.
// Random
// demonstrates generating random numbers
int randNumber;
void setup()
{
Serial.begin(500000);
while(!Serial);
// Print random numbers with no seed value
Serial.println("Print 20 random numbers between 0 and 9");
for(int i=0; i < 20; i++)
{
randNumber = random(10);
Serial.print(randNumber);
Serial.print(" ");
}
Serial.println();
Serial.println("Print 20 random numbers between 2 and 9");
for(int i=0; i < 20; i++)
{
randNumber = random(2,10);
Serial.print(randNumber);
Serial.print(" ");
}
// Print random numbers with the same seed value each time
randomSeed(1234);
Serial.println();
Serial.println("Print 20 random numbers between 0 and 9 after constant seed ");
for(int i=0; i < 20; i++)
{
randNumber = random(10);
Serial.print(randNumber);
Serial.print(" ");
}
// Print random numbers with a different seed value each time
randomSeed(analogRead(0)); // read from an analog port with nothing connected
Serial.println();
Serial.println("Print 20 random numbers between 0 and 9 after floating seed ");
for(int i=0; i < 20; i++)
{
randNumber = random(10);
Serial.print(randNumber);
Serial.print(" ");
}
Serial.println();
Serial.println();
}
void loop()
{
}
The following code is based on the function from Denois90 in post #38 of my earlier thread
https://forum.arduino.cc/t/use-pin-for-both-input-and-output/1003556/37
I don't yet understand the EEPROM stuff, but was impatient to move on. It obviously remains reliant on the 'weak' analog inputs for its seeds, but for my simple application I'm happy enough with its results. Of course, I still need to add some logic to avoid repeating a folder within the current session.
// Thread https://forum.arduino.cc/t/use-pin-for-both-input-and-output/1003556/37
// Code from Danois90, Saturday 18 June 2022
#include "EEPROM.h"
#define SEED_ADDR 0
int randNumberF;
void setup()
{
Serial.begin(115200);
Serial.println("RandomSeedWithEEPROM_A6A7-2");
Serial.println(F(""));
Serial.print(F("Random numbers"));
}
void loop()
{
randNumberF = random(1, 29); // Chooses folder 1 to 28
Serial.println(randNumberF);
delay(500); // Wait before printing another set
}
void init_random_seed()
{
int seed;
EEPROM.get(SEED_ADDR, seed);
seed += analogRead(A6) + analogRead(A7); //You could use different and/or more/less pins
EEPROM.put(SEED_ADDR, seed);
randomSeed(seed);
}type or paste code here
Here's the first 63 results:
14, 8, 24, 21, 23, 9, 25, 27, 27, 11, 19, 18, 7, 9, 19, 3, 26, 8, 17, 2, 16, 20, 10, 12, 23, 16, 7, 19, 9, 17, 8, 3, 15, 2, 9, 1, 2, 20, 1, 4, 11, 12, 11, 19, 12, 21, 12, 23, 16, 23, 9, 21, 26, 2, 11, 15, 13, 18, 22, 14, 17, 17, 10
Thanks to all those who offered practical help.
EDIT:
Duh! That was a premature celebration. I've plainly not implemented Danois90's function correctly. As I'm sure someone is about to point out, I'm getting no seeding whatsoever. Not surprisingly, as my code doesn't actually call the function!
EDIT
Now fixed, and getting the expected different sets on each startup.
Correction: folder 1 to 27.
Thanks, duly corrected.
Here is the output from your sketch copied from the Serial monitor using Ctrl+A then Ctrl+C
I had no trouble copying it
Print 20 random numbers between 0 and 9
7 9 3 8 0 2 4 8 3 9 0 5 2 2 7 3 7 9 0 2
Print 20 random numbers between 2 and 9
9 3 7 7 2 7 5 8 2 9 3 4 2 5 4 3 5 7 5 7
Print 20 random numbers between 0 and 9 after constant seed
8 2 8 7 1 8 0 3 6 5 9 0 3 4 3 1 2 3 9 4
Print 20 random numbers between 0 and 9 after floating seed
9 1 8 5 0 8 6 2 7 2 8 0 2 0 6 8 6 9 8 3
For completeness, here is a screenshot of the same Serial monitor window
Thanks for testing. I have several recent test sketches and can't confidently identify that one, so please advise its name or post the code.
I'm generally successful in printing now, providing I take the precautions I mentioned.
EDIT:
I found that sketch ch03_random2. I'd downloaded and deleted.
My results were not as clean as yours. Presumably because I printed directly after upload?
RUN #1 (from upload)
13:49:14.671 -> ?d??.????"2???\??????????????????Q?Print 20 random numbers between 0 and 9
13:49:14.671 -> 7 9 3 8 0 2 4 8 3 9 0 5 2 2 7 3 7 9 0 2
13:49:14.671 -> Print 20 random numbers between 2 and 9
13:49:14.671 -> 9 3 7 7 2 7 5 8 2 9 3 4 2 5 4 3 5 7 5 7
13:49:14.671 -> Print 20 random numbers between 0 and 9 after constant seed
13:49:14.671 -> 8 2 8 7 1 8 0 3 6 5 9 0 3 4 3 1 2 3 9 4
13:49:14.671 -> Print 20 random numbers between 0 and 9 after floating seed
13:49:14.671 -> 7 3 2 2 1 4 6 7 2 7 7 5 8 6 8 8 4 1 7 4
13:49:14.671 ->
Copy/paste OK
--------------------
RUN #2 (from upload, to fresh monitor)
13:51:28.281 -> ?"g???L?????????????????I?>d
Copy/paste failed
Copy/paste OK with first line not selected
--------------------
RUN #3 (no upload, just fresh monitor)
13:54:59.905 -> Print 20 random numbers between 0 and 9
13:54:59.905 -> 7 9 3 8 0 2 4 8 3 9 0 5 2 2 7 3 7 9 0 2
13:54:59.905 -> Print 20 random numbers between 2 and 9
13:54:59.905 -> 9 3 7 7 2 7 5 8 2 9 3 4 2 5 4 3 5 7 5 7
13:54:59.905 -> Print 20 random numbers between 0 and 9 after constant seed
13:54:59.905 -> 8 2 8 7 1 8 0 3 6 5 9 0 3 4 3 1 2 3 9 4
13:54:59.905 -> Print 20 random numbers between 0 and 9 after floating seed
13:54:59.905 -> 3 6 8 6 5 5 5 6 1 9 3 2 0 9 1 9 5 0 3 7
13:54:59.905 ->
Copy/paste OK
Tentative conclusion; printing directly after upload is unreliable
--------------------
The junk output at the start of the printout depends on which Arduino board you are using and has a number of different causes such as
- junk values being present in the Serial buffer caused by the Serial interface being used to upload the code
- junk values being present in the Serial buffer left over from previous output
- the upload process outputting information but at a different baud rate than that set in the Serial monitor. In my experience this is most common when using ESP based systems
There is also a difference between uploading code with the Serial monitor open and uploading the code then opening the Serial monitor. Using a Uno as you are, then opening the Serial monitor causes an explicit reset at that time but I don't know when the reset occurs when you upload code
I’ve had helpful further discussions on randomness for my music box project with @Danois90 and @aag in that parallel thread.
