-1

If I have a bunch of if statements:

  1. if(){}
  2. if(){}
  3. if(){}
  4. if(){} etc....

Is there a way that these if statements can be run randomly instead of top to bottom? For example, if() statement 4 may run first and if() statement 2 may run last and other possibilities.

Red
  • 115
  • 8

1 Answers1

1

Shuffle an array of 0...n integers. Each number represents one of your if-statements.

Random shuffling of an array

int[] indices = shuffleRandomArray(0, 10);
for(int idx=0; idx<indicies.length; idx++) {
  int val = indicies[idx];
  if(val==0) if(){..};
  else if(val==1) if(){..};
  else if(val==2) if(){..};
  else if(val==3) if(){..};
  else if(val==4) if(){..};
  ...
}

In Java 8 you could use function pointers https://www.baeldung.com/java-8-double-colon-operator

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Whome
  • 10,181
  • 6
  • 53
  • 65