Generate a randomised set from a variable amount of consecutive numbers

Hi,
I humbly apologise if this has been answered, but I cannot seem to get the code to work.

I need a variable n to be the length of a set of numbers that starts at 0 and goes to n-1; then I need to randomise the resulting array, so that a sequence made of those numbers is n long, truly random and with no repeats.
The code below works fine with a fixed array (**int randomVals[24]**with n substituted by the 24).

The game needs to allow for the value of n be anywhere from 2 to 24, chosen at start time.

I have tried with the following code snippet, but I'm getting hung on setting up an array that has n elements:

int n = 10;
int randomVals[n]; // hangs on this line

void setup() { 
  int temp;
  int passCounter = 0;
  int elementsFilled = 0;
  
  
  randomSeed(analogRead(0));   // Seed generator...

  Serial.begin(9600); 
   
  while (true) {
    temp = random(0, n);
    if (randomVals[temp] == 0) {         // If this number has not been generated yet...
      randomVals[temp] = passCounter++;  // ...write its value here.
    }
    if (passCounter > (n-1)) {               // If we've filled in all elements, leave...
      break;
    }
  }
  
  for (int i = 0; i < n; i++)
    Serial.println(randomVals[i]);
}
void loop()
{
}

You can't declare a variable length array in C. It's considered variable because you could change the value of int variable "n" at some other place in your program. Try

#define n (10)
int randomVals[n]; // hangs on this line

Please find a better name for the constant than "n".

aarg:
Please find a better name for the constant than "n".

I'm sure the variable name in my example is really hurt that you dislike it so :confused:
I simplified for expediency, and of course the actual code will have a name like amountOfPositions, but to make it easier to read I stripped the snippet out to the kernel.

aarg:
You can't declare a variable length array in C. It's considered variable because you could change the value of int variable "n" at some other place in your program.

But I need to be able to choose the amount of positions at run-time as a user input:
If there are 3 positions I need to randomise 3. If there are 12, so 12 needs to be randomised.
Once running, after that choice, the result is fixed and will not be open to being altered until runtime comes around next time.

OldManNoob:
I simplified for expediency, and of course the actual code will have a name like amountOfPositions, but to make it easier to read I stripped the snippet out to the kernel.
But I need to be able to choose the amount of positions at run-time as a user input:
If there are 3 positions I need to randomise 3. If there are 12, so 12 needs to be randomised.
Once running, after that choice, the result is fixed and will not be open to being altered until runtime comes around next time.

Then you need to declare an array of the largest size you will ever use at compile time, and only use the number you need at run time.

By the way, the word "amount" is usually used for continuous quantities like measurements of water, where "number" means something that you can count. In this case, "numberOfpositions" makes more sense.

For example:

#define MAXNUMBEROFVALUES (100)
int numberOfpositions = 8;
int randomVals[MAXNUMBEROFVALUES];
...
  while (true) {
    temp = random(0, numberOfpositions);

Divest yourself of the concept that "n" is easier to read than "numberOfpositions" right now.

You could allways allocate and start with an array of 24 elements.
When you know the size you can randomize a part of it you need for the game.

I added a sketch to show what I mean, please give it a try.

int array[24];
int size = 12;

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  
  // setup array
  for (int i = 0; i < 24; i++)
  {
    array[i] = i;
  }
  
  // seed random generator 
  
  // ask size 
  
  // randomize size elements
  int idx = 0;
  int tmp = array[0];
  for (int i = 0; i < size; i++)
  {
    int n = random(size);
    array[idx] = array[n];
    idx = n;
  }
  array[idx] = tmp;
  
  // show the partial randomization
  for (int i = 0; i < size; i++)
  {
    Serial.print(array[i]);
    Serial.print('\t');
  }
  Serial.println();
}

void loop()
{}

robtillaart:
You could allways allocate and start with an array of 24 elements.
When you know the size you can randomize a part of it you need for the game.

Thank you Rob, that's perfect. It allows me to grab an input and implement the game based on the result.
And I can go further!
In my case I'll be dynamically pinging the stations at boot in setup, and allocating those that respond to the count so I can build my randomising around the result.
This solution will allow for a situation where ID can be fixed for each station, but the user need not be concerned with making sure the 'correct' ones are used. For example: Station 1, 3, 9, 22 deployed; randomise that group; address them once each in random order. Result!

aarg:
Then you need to declare an array of the largest size you will ever use at compile time, and only use the number you need at run time.

#define MAXNUMBEROFVALUES (100)

int numberOfpositions = 8;
int randomVals[MAXNUMBEROFVALUES];
...
  while (true) {
    temp = random(0, numberOfpositions);


Divest yourself of the concept that "n" is easier to read than "numberOfpositions" right now.

And this works too aarg, thank you.

#define MAXCOUNT (10)
uint8_t randomVals[MAXCOUNT];
uint8_t numberInputByCheckingPositions = 6;

OldManNoob:
Thank you Rob, that's perfect. It allows me to grab an input and implement the game based on the result.
And I can go further!
.....
. Result!

Please post your final solution when it works, so it can be used for future reference,

Thanks