Random selection excluding contents of ring buffer

Hi,

I am trying to randomly choose a value between 1 and 10 every 5 seconds without selecting any of the previous 3 values.
my idea is to save the previous values in a small ring buffer and choose random values and discard them if the values match the current values in the ring buffer.

however, this seems wasteful and could potentially get stuck. Currently the code doesn't work correctly anyway so I think its time to ask for help.

I cant think of a better way but know there must be one..

Any advice on improving my technique and achieving the desired result would be appreciated.

#include <FastLED.h> 
// PATTERN SELECTION
int Pattern = 1;                                // Stores the current Pattern
int PatternMax = 10;                            // adjust this for the max number of patterns                                         
int PatternMin = 1;                             // keep this 1 so patterns cycle in a loop 
int PatternMaxRandomMode = 7;                   // stop the solid colours from apearing in the random and cycle modes. this is so patterns > PatternMaxRandomMode can be excluded from the random 


int PrevPatterns[3]= {0,0,0};
int PrevPatternCount = 0; 

// timer for pattern mode change 
int Pattern_period = 5;                     // this is the time period between pattern changes  

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);                             // start serial port
}

void loop() {
EVERY_N_SECONDS(Pattern_period){
      
      PrevPatterns[PrevPatternCount] = Pattern;                     // save current patern 
      PrevPatternCount++;                                           // increment count
      if (PrevPatternCount >= sizeof(PrevPatterns)){                
        PrevPatternCount = 0;                                       // loop array count                     
      }
      Serial.println(" ");
      Serial.println("///////////////////////////////");
      Serial.print("Previous Pattern 1 = ");
      Serial.println(PrevPatterns[PrevPatternCount]);  
      Serial.print("Previous Pattern 2 = ");
      Serial.println(PrevPatterns[PrevPatternCount-1]); 
      Serial.print("Previous Pattern 3 = ");
      Serial.println(PrevPatterns[PrevPatternCount-2]); 

      if(Pattern == PrevPatterns[0]||Pattern == PrevPatterns[1]||Pattern == PrevPatterns[2]){
      
      Pattern = random(PatternMin,PatternMaxRandomMode+1);            // randomly choose Pattern after Pattern_period time can have same pattern more than once in a row... PatternMaxRandomMode
      Serial.print("Chooseing Pattern = ");
      Serial.println(Pattern);  
      }
      Serial.print("Current Pattern = ");
      Serial.println(Pattern);  
    }                                              

}

Not sure if this is your problem but the Arduino Ref states that random is a long.

thanks,
I dont think thats part of my problem. Pattern is integer so i assume the results of Pattern = random(); are cast as an int?

but im a noob so what would i know lol

Some folks here can look at the code and immediately have a mental image of the flow. I can't. So when I get into such a situation I draw a flow chart. I have a graphic mind and issues with a flowchart jump out to me.
Perhaps drawing one might help you.

You never described what actually happens. Does it even compile without errors?

This may not work as you appear to want because this function returns the number of bytes occupied by the array, not the number of elements. You can, in this case, divide the result by sizeof( int ) to get the number of elements.

Edit:
You also need a while loop. The function random() could theoretically , on subsequent calls, return a series of values already in your array . It may require multiple attempts to get a value not currently in the array. The while condition would be very similar to your current if condition.

I am a flow chat pencil and pad type of guy so I'm on your page lol

Sorry that was an obvious oversight on my part.

here is the serial output of the first 20 seconds.

current pattern should never = any of the previous patterns but clearly it does and previous pattern 1 is all kinds of messed up, i am guessing thats because it is overflowing ?

15:03:19.566 -> ///////////////////////////////
15:03:19.613 -> Previous Pattern 1 = 0
15:03:19.613 -> Previous Pattern 2 = 3
15:03:19.613 -> Previous Pattern 3 = 1
15:03:19.613 -> Chooseing Pattern = 7
15:03:19.613 -> Current Pattern = 7
15:03:24.569 ->
15:03:24.569 -> ///////////////////////////////
15:03:24.569 -> Previous Pattern 1 = 1061159404
15:03:24.604 -> Previous Pattern 2 = 7
15:03:24.604 -> Previous Pattern 3 = 3
15:03:24.604 -> Chooseing Pattern = 2
15:03:24.604 -> Current Pattern = 2
15:03:29.572 ->
15:03:29.572 -> ///////////////////////////////
15:03:29.572 -> Previous Pattern 1 = 0
15:03:29.572 -> Previous Pattern 2 = 2
15:03:29.607 -> Previous Pattern 3 = 7
15:03:29.607 -> Current Pattern = 2
15:03:34.588 ->
15:03:34.588 -> ///////////////////////////////
15:03:34.588 -> Previous Pattern 1 = 255
15:03:34.588 -> Previous Pattern 2 = 2
15:03:34.588 -> Previous Pattern 3 = 2
15:03:34.588 -> Current Pattern = 2

*Chosing

thanks I will look into this as i was expecting the number of elements as you suggest

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