I need to generate several unique numbers per day (approx. 15-20). If the unique numbers repeat the following day, no matters because I keep truck of them using datetime.
The following code fails to generate the needed unique numbers. They're days that the code works and they're days which the unique numbers are duplicated.
I increased the range from 100 to 300 to reduce the duplicity but it hasn’t helped much. See attached img.
Is there a way to ensure that a random number is not duplicated?
TIA
long randNumber; //var for random number
void setup()
{
Serial.begin(9600);
randomSeed((analogRead(A0) + analogRead(A1)) / 2); //pins A0 and A1 are not used
}
void loop() {
randNumber = random(0, 300);
Serial.println(randNumber);
delay(2000);
}
Is there a way to ensure that a random number is not duplicated?
If the numbers generated were truly random then you could get any sequence of numbers in the range specified including duplicates and even runs of the same number.
Check whether the number has already been generated and reject it if it has.
I need to generate several unique numbers per day (approx. 15-20). If the unique numbers repeat the following day, no matters because I keep truck of them using datetime.
Must they also be random? It's easy to generate numbers that are just unique. Simply start from 0, and increment by 1 each time you need a new number. That guarantees that you will never get a repeat until you overflow the number type.