I am trying to set the background color of a view using a timer to change the color every few seconds, but often a color is set twice in a row due to the fact that the random number referring to a specific color is generated twice. How can I change the code to avoid generating the same random number twice in a row?
final Runnable runnable = new Runnable() {
@Override
public void run() {
Random rand = new Random();
int n = rand.nextInt(3);
n += 1;
switch (n){
case 1:
colorView.setBackgroundColor(Color.GREEN);
break;
case 2:
colorView.setBackgroundColor(Color.MAGENTA);
break;
case 3:
colorView.setBackgroundColor(Color.CYAN);
break;
}
}
};