Generate array with random values

Hello,

I am trying to generate an array of 12 random numbers (integers) from 1-12 and there are no double values allowed (so each number from 1-12 should be in the array). I have tried a a couple of things but somehow double numbers keep being generated. Any ideas? Please help!

Lotte

Hi Lotte,

you make an array filled with the numbers 1…12 and swap randomly pairs. I have chosen for generating the pairs to take the index i, but you could also generate 2 random numbers and swap those. The advantage of this version is that it is a bit faster.

int array[12] = {1,2,3,4,5,6,7,8,9,10,11,12 } ;

void setup()
{
  Serial.begin(115200);

  // next 3 lines are for randomizing the randomgenerator.
  Serial.println("press a key");
  while (Serial.available() ==0);
  randomSeed(micros() +  Serial.read());
  
  // this is the trick
  for (int i= 0; i< 12; i++) 
  {
    int pos = random(12);
    int t = array[i];   
    array[i] = array[pos];
    array[pos] = t;
  }

  for (int i= 0; i< 12; i++)
  {
    Serial.print(i);
    Serial.print(": ");
    Serial.println(array[i]);
  }
}

void loop(){}

I don't think this code generates 12 unique numbers...

I don't think this code generates 12 unique numbers...

The array starts with 12 unique numbers. The code simply shuffles the order of elements in the array. At the end, the elements still contain unique values.

I don’t think this code generates 12 unique numbers…

Lotte, you are 100% right, the code does not generate 12 unique numbers, I hardcoded them unique in the array. And as PaulS correctly states, I shuffle the contents of the array randomly. This gives you very fast a random array with those 12 unique number. (you can use 12 other numbers if you like)

If you want to generate the numbers randomly, and check if they are allready in the array, give the code below a try and check the difference in terms of speed,
then try changing SIZE to 50,200 or 800 to see the scalability. Furthermore look at the difference in footprint

#define SIZE 12    // change this to 800

int array[SIZE] ;
int count = 0;

unsigned long tim = 0;

void setup(){
  Serial.begin(115200);

  // next 3 lines are for randomizing the randomgenerator.
  // Serial.println("press a key");
  //while (Serial.available() ==0);
  // randomSeed(micros() +  Serial.read());

  tim = millis();
  boolean done = false;
  while (!done)
  {
    // generate random number
    int x = random(SIZE)+1;  

    // is it in the array allready
    boolean exists = false;
    for (int i= 0; i< count && exists == false; i++) 
    {
      if (x == array[i]) exists = true;
    }
    if (exists == false)
    {
      array[count] = x;
      count++;
    }
    done = (count == SIZE) ;
  }
  tim = millis() - tim;
  Serial.print("Time used 1: ");
  Serial.println(tim);

//  for (int i= 0; i< SIZE; i++)
//  {
//    Serial.print(i);
//    Serial.print(": ");
//    Serial.println(array[i]);
//  }

  tim = millis();

  for (int i= 0; i< SIZE; i++) 
  {
    int pos = random(SIZE);
    int t = array[i];   
    array[i] = array[pos];
    array[pos] = t;
  }
  tim = millis() - tim;
  Serial.print("Time used 2: ");
  Serial.println(tim);  
  
//  for (int i= 0; i< SIZE; i++)
//  {
//    Serial.print(i);
//    Serial.print(": ");
//    Serial.println(array[i]);
//  }
}

void loop(){
}

lotte1990: I am trying to generate an array of 12 random numbers (integers) from 1-12 and there are no double values allowed (so each number from 1-12 should be in the array).

I must be missing something - if you want 12 random integers from 1-12 without duplicates, you MUST use the numbers 1-12, and robtillaart's first solution works fine. Why didn't you like his code? If it's because of the shuffling, the random values used there makes the list random.