# Need help understanding how to use code

Hi,

I am trying to use the Random Hat example posted on the site. Here is the code as provided by Paul Badger:

/* 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 1
#define randomHatEndNum 4
#define NumInHat abs(randomHatEndNum - randomHatStartNum) + 1

int i, x;

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

void loop()
{ // randomHat test
for (i=1; i<=NumInHat; 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 *= randomHatStartNum + i; } *

• else if (randomHatStartNum > randomHatEndNum){ // defensive programming in case startNum is greater than endNum*
_ randArray = 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 = randArray[i + 1]; // bump all the higher array contents down one, erasing the last number chosen
* }
currentNumInHat–; // decrement counter*
* return thePick;
}*
OK, the code runs as posted, however, it continues to run forever since the ‘call’ to the function is in the void loop () section. Normally this is good, however I only want to run the call one time and then I will store the results for later use. I tried to simply move it up into the void setup () section, however the compiler just gave me errors.
So now is there some way to leave the ‘call’ in the void loop () section and run it only once? I believe someone has faced this before and has figured out out how to accomplish this task.
Thanks in advance for your help and to Paul for posting the example code._

You should be able to move the loop() code into setup() and run it once. I do see several places where it appears that you dropped an index into the array. You have:

`````` randArray = randomHatStartNum + i;
//
randArray = randomHatEndNum + i;
//
randArray =  randArray[i + 1];
``````

in the program and I don't know how it can compile without modifications similar to:

`````` randArray[i] = randomHatStartNum + i;
//
randArray[i]  = randomHatEndNum + i;
//
randArray[i]  =  randArray[i + 1];
``````

After these fixes, I moved the loop() code into setup() and it compiled and ran. (I assume it ran correctly.)

Also, I know that you are new to the Forum...and welcome, but we do ask you to read the first two posts at the top of the Forum and following the guidelines therein, especially the use of code tags when listing code.

Instead of writing random nonsense, you will save time by learning the basic syntax and usage of C/C++.

I do see several places where it appears that you dropped an index into the array. You have:

No, that happened because the code was NOT posted correctly, in code tags. The [ i ] sequence of characters is the tag to start italicizing text. When [ i ] happens inside code tags, it does not mean to start italicizing.