Random number in array

Hi all. I'm trying to add random non-repeat numbers to an array but I can't seem to get it to work the first time through the loop. It takes a few times through to complete the list. Can someone give me some advice? Thanks

#define SIZE 5 

int array[SIZE] ;
int count = 0;
int intRan = 0;
bool boolExists = false;
bool boolRunOnce = false;
unsigned long tim = 0;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < SIZE; i++) {
    array[i] = -1;
  }
}

void loop() {
  if (not boolRunOnce) {
    for (int i = 0; i < SIZE; i++) {
      intRan = random(0, SIZE);
      for (int x = 0; x < SIZE; x++) {
        int intRead = array[x];
        if (intRead == intRan) {
          boolExists = true;
        }
      }

      if (not boolExists) {
        array[i] = intRan;
      }
      boolExists = false;

    }

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

And what happened to the forum lol? I'm not fond of the new template but maybe I just need to get used to it.

  1. Fill the array with consecutive numbers
  2. Iterate through the array, and swap each entry with another one chosen at random.

agree with @anon57585045, since you have all the numbers between 0 and SIZE-1 in your array of size SIZE, the simplest way that will lead to guaranteed timing is to fill the array in sequential order and then shuffle.

There is a well know shuffle algorithm called the Fisher–Yates shuffle

try this:

const int SIZE = 10;
int array[SIZE] ;

// Fisher–Yates shuffle
template<typename T>
void shuffleArray(T* a, const size_t size) {
  for (size_t i = size - 1; i > 0; --i) {
    size_t r = random(0, i + 1); // generate rand numbers from 0 to i
    T tmpSwap = a[i];
    a[i] = a[r];
    a[r] = tmpSwap;
  }
}

void printArray(const int* a, const size_t size) {
  for (size_t i = 0; i < size; i++) {
    Serial.print(a[i]); Serial.write(' ');
  }
  Serial.println();
}

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

  // randomize
  randomSeed(analogRead(A0));

  // fill the array in sequential order
  for (int i = 0; i < SIZE; i++) array[i] = i;
  printArray(array, SIZE);

  // shuffle the array
  shuffleArray(array, SIZE);
  printArray(array, SIZE);
}

void loop() {}

or if template is intimidating

const int SIZE = 10;
int array[SIZE] ;

// Fisher–Yates shuffle
void shuffleArray(int* a, const size_t size) {
  for (size_t i = size - 1; i > 0; --i) {
    size_t r = random(0, i + 1); // generate rand numbers from 0 to i
    int tmpSwap = a[i];
    a[i] = a[r];
    a[r] = tmpSwap;
  }
}

void printArray(const int* a, const size_t size) {
  for (size_t i = 0; i < size; i++) {
    Serial.print(a[i]); Serial.write(' ');
  }
  Serial.println();
}

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

  // randomize
  randomSeed(analogRead(A0));

  // fill the array in sequential order
  for (int i = 0; i < SIZE; i++) array[i] = i;
  printArray(array, SIZE);

  // shuffle the array
  shuffleArray(array, SIZE);
  printArray(array, SIZE);
}

void loop() {}

Why not make both functions templates? (or neither as in your second example)

Caught me :slight_smile:

I copied the template one from one of my existing code and was lazy for the printing which I added…

Hello
Take this sketch. Try and check it out for your project.

constexpr int SizeOfArray {10};
int randomNumbers[SizeOfArray];
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
  for (auto &randomNumber : randomNumbers) randomNumber = -1;
  int write_ = 0;
  do {
    int random_ = random(0, SizeOfArray);
    bool douple = false;
    for (int read_ = 0; read_ < write_ + 1; read_++) if (random_ == randomNumbers[read_]) douple = true;
    if (!douple) randomNumbers[write_++] = random_;
  } while (!(write_ == SizeOfArray));
  for (auto randomNumber : randomNumbers) Serial.print(randomNumber),  Serial.print(F(" "));
  Serial.println(F(" "));
}
void loop() {

}

Have nice day and enjoy coding in C++.

What does it mean? Do you want to:

  1. Randomly shuffle an array of unique numbers?
  2. Generate different arrays of random numbers every time the programs runs?

In either case, you must collect entropy first. The usual way to do it is to seed the array generator with a reading from a floating analog pin.

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