# Resize Array? RandomHat Function

I translated into C a function I’ve found useful for a number of projects. It simply picks a random number “out of a hat”, choosing all the numbers before repeating one. I think it might be useful to be able to change the size variables on the fly, but haven’t been able to determine how to do this in C.

The array is declared static, so the memory should be freed and the array redeclared I think. Anyone care to point me in the right direction?

``````/* RandomHat
choose one from a hat of n choices each time through loop
Choose each number exactly once before reseting and choosing again
*/

#define randomHatStartNum 0
#define randomHatEndNum 10
#define NumberInHat abs(randomHatEndNum - randomHatStartNum) + 1

int i, x;

void setup()
{
Serial.begin(9600);
Serial.println("start ");
}

void loop()
{        // randomHat test
for (i=1; i<=NumberInHat; i++){
x = randomHat();
Serial.print(x);
Serial.print(" ");
delay(100);
}
Serial.println(" ");
}

int randomHat(){
static int  totalNumInHat = abs(randomHatEndNum - randomHatStartNum) + 1;
static int currentNumInHat = 0;
abs(randomHatEndNum - randomHatStartNum) + 1;
static int randArray[abs(randomHatEndNum - randomHatStartNum) + 1];
int i;            //as long as they can be changed without harming other code
int thePick;            //this is the return variable with the random number from the pool
int theIndex;

if  (currentNumInHat == 0){                      // hat is emply - all have been choosen - fill up array again
for (i = 0 ; i<=(totalNumInHat - 1); i++){  // Put 1 TO numberInHat in array - starting at address 0.
if (randomHatStartNum < randomHatEndNum){
randArray[i] = randomHatStartNum + i; }
else if (randomHatStartNum > randomHatEndNum){  // defensive programming in case startNum is greater than endNum
randArray[i] = randomHatEndNum + i; }
else{
return randomHatStartNum; }               // startNum and endNum must be same number - return one - and bail out
}
currentNumInHat = abs(randomHatEndNum - randomHatStartNum) + 1;   // reset current Number in Hat
//if something should happen when the hat is empty do it here
}

theIndex = random(currentNumInHat);                   //choose a random index
thePick = randArray[theIndex];

for (i = theIndex; i<= (currentNumInHat - 1); i++){
randArray[i] =  randArray[i + 1];                           // bump all the higher array contents down one,  erasing the last number chosen
}
currentNumInHat--;                                          // decrement counter
return thePick;
}
``````

Check out malloc() and free().

The more I thought about it, the more it didn't seem to make sense to start resizing the array on the fly. Also I failed to think of a use except for convenience in original parameter.

If someone did put the function in a loop and change the parameter often, it could lead to memory issues, possible hard-diagnose bugs it seems to me.

Paul