0

So I was looking to randomize the way certain methods are called, so that each one is only called once per instance and every single method is called.

So say one instance they are called in the order:

method2 method4 method3 method1

but in the next instance they are called in a different order:

method3 method2 method1 method4

The code that I have to randomize the order looks like this:

public void randomCalls(){
    int[] order = new int[4];

    for(int i=0; i<order.length; i++){
        order[i]=nextNumber(order);
    }
}

public int nextNumber(int[] array){
    Random r = new Random();
    int x = r.nextInt();
    for(int i=0; i<array.length; i++){
        if(arrayHasNumber(array,x)){
            x = nextNumber(array);
        }
    }
    return x;
}

public boolean arrayHasNumber(int[] array, int x){
    for(int i=0;i<array.length;i++){
        if(array[i]==x){
            return true;
        }
    }
    return false;
}
jocopa3
  • 796
  • 1
  • 10
  • 29
  • 2
    My initial instinct would be a switch statement based on a random number and status flags to indicate what methods have been called. – Aurand Mar 28 '13 at 05:02
  • @jocopa3 - You've coded a hell lot just to randomize. This is not good. Just see if the answer I posted eases your task. Its a pretty basic solution. – Rahul Mar 28 '13 at 05:17

4 Answers4

2

Based on @Aurand suggestion, you can have a switch that will call your methods and a List<Integer> that will contain the indexes of the methods you want to invoke, then shuffle the list elements using Collections.shuffle and calling the switch to call your methods. Code sample:

final int METHODS_QUANTITY = 4;
List<Integer> lstIndexes = new ArrayList<Integer>();
for(int i = 1; i <= METHODS_QUANTITY; i++) {
    lstIndexes.add(i);
}
//you can change the condition for the number of times you want to execute it
while(true) {
    Collections.shuffle(lstIndexes);
    for(Integer index : lstIndexes) {
        switch(index) {
            case 1: method1(); break;
            case 2: method2(); break;
            case 3: method3(); break;
            case 4: method4(); break;
        }
    }
}

Still, the question stands: why would you need this on real world application?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Something like

LinkedList methods
methods.add(1)
methods.add(2)
....

for(i=0; i<methods.size;i++)
r = random.next(methods.size)
switch(methods.get(r)) {
case 1: method1()
case 2: method2()
...
methods.remove(methods.get(r)
1

My tip would be to go for an ArrayList & thrown in all the method names during initialization.

Then get a random number using random(list.size()) and pop that element out from the ArrayList.

Use a switch case, and whatever method name has popped out, call that method.

Keep doing this, till the list becomes empty.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Possibly you must retain the states (orders in which the calls were made) in an internal variable or in an array (in case you want to have all of them). And then tune your call site to use this state variable.

TJR
  • 58
  • 4