Random assign methods into inputs

2019-08-28 21:57发布

For each of the inputs (name, phoneno), I am using Random Number Generator to assign the inputs to a method. My goal is to get all possible combinations of methods to an input.

Below are my codes. What if I have 1000 of inputs and methods? Is there any efficient way to this this?

public static void main(String[] args) {

  for (int i = 0; i < 9; i++) {

    Random myRand = new Random();
    int randomInteger = myRand.nextInt(10);

    if (randomInteger == 0) {
      name = methodA();
      phoneno = methodA();
    } else if (randomInteger == 1) {
      name = methodA();
      phoneno = methodB();
    } else if (randomInteger == 2) {
      name = methodB();
      phoneno = methodB();
    } else if (randomInteger == 3) {
      name = methodB();
      phoneno = methodA();
    } else if (randomInteger == 4) {
      name = methodC();
      phoneno = methodC();
    } else if (randomInteger == 5) {
      name = methodC();
      phoneno = methodA();
    } else if (randomInteger == 6) {
      name = methodC();
      phoneno = methodB();
    } else if (randomInteger == 7) {
      name = methodA();
      phoneno = methodC();
    } else if (randomInteger == 8) {
      name = methodB();
      phoneno = methodC();
    }
  }
}

public static String methodA() {
  //do something
}

public static String methodB() {
  //do something
}

public static String methodC() {
  //do something
}

Any help will be appreciated. Thank You

标签: java random
3条回答
叼着烟拽天下
2楼-- · 2019-08-28 22:13

Here is a solution...

import java.util.Random;

public class Main {

    public static final int FUNCTION_NUMBER = 3;

    public static String functionA() {
        return "A";
    }

    public static String functionB() {
        return "B";
    }

    public static String functionC() {
        return "C";
    }

    public static String callFunction(int function_index) {
        switch (function_index) {
            case 0: return functionA();
            case 1: return functionB();
            case 2: return functionC();
            default: throw new IllegalArgumentException("Incorrect value for function_index");
        }
    }

    public static void main(String[] args) {
        Random random = new Random();

        int n = random.nextInt((FUNCTION_NUMBER * FUNCTION_NUMBER) - FUNCTION_NUMBER);
        int name_function_index = n / FUNCTION_NUMBER;
        int phone_no_function_index = n % FUNCTION_NUMBER;

        System.out.print(callFunction(name_function_index));
        System.out.print(callFunction(phone_no_function_index));
        System.out.println("--------------");

    } 

}

If you want to add an other function, just declare it, increment the FUNCTION_NUMBER constant and add a case in the callFunction function.

All the job is done here

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER);
int name_function_index = n / FUNCTION_NUMBER;
int phone_no_function_index = n % FUNCTION_NUMBER;

Considering that you have 3 functions in your example, we have 9 combinations (AA, AB, AC, BA, BB, BC, CA, CB, CC). n is a random value between 0 and 8 included.

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER);

The number of combinations can be splitted in groups. One group for one name function. To do that we use the division operator. So, for the values 0, 1 and 2 the group number is 0 (functionA). For the values 3, 4 and 5 the group number is 1 (functionB) and for the value 6, 7 and 8.

int name_function_index = n / FUNCTION_NUMBER;

For the phone number function, it's like we iterate over the functions for each name function (for each group like described previously). To do that we use the modulo operator.

int phone_no_function_index = n % FUNCTION_NUMBER;

Here is a table of the functions indexes for each value of n. You can see that it represents all the functions combinations.

n name_function phoneno_function

0 0 0
1 0 1
2 0 2
3 1 0
4 1 1
5 1 2
6 2 0
7 2 1
8 2 2

If you want to discard the duplications, you can use this

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER) - FUNCTION_NUMBER;
int name_function_index = n % FUNCTION_NUMBER;
int phone_no_function_index = (n + 1 + (n/4)) % FUNCTION_NUMBER;
查看更多
【Aperson】
3楼-- · 2019-08-28 22:18

You could use a switch statement

    int number=myRand.nextInt(10);
        switch(number){
            case 0:
            case 1:
            case 7:
            name=methodA();
        break;
        case 2:
        case 3:
        case 8:
        name=methodB();
        //repeat for C




                }
switch(number){
            case 0:
            case 3:
            case 5:
            phoneno=methodA();
        break;

        //repeat for B and C




                }
查看更多
Rolldiameter
4楼-- · 2019-08-28 22:24

Store your methods in an array and take a random array element for each of name and phone number. It’s probably easiest using a functional interface, so let’s first declare

@FunctionalInterface
public interface InputMethod {

    String method();

}

I am assuming your methods return strings, but any other type will work too.

With the above interface you can do for example:

    InputMethod[] methods = {
            () -> "A",
            () -> LocalTime.now(ZoneId.of("Asia/Novosibirsk")).toString(),
            () -> "42",
            SomeOtherClass::someLongAndComplicatedMethod
    };

    Random myRand = new Random();
    int randomInteger = myRand.nextInt(methods.length);
    String name = methods[randomInteger].method();
    randomInteger = myRand.nextInt(methods.length);
    String phoneno = methods[randomInteger].method();

    System.out.println(name + ' ' + phoneno);

Example output:

16:58:16.404504 16:58:16.404784

In this particular run LocalTime.now happened to be used for both name and number. I wanted to show this one to you because the times are not exactly the same since the method was called twice in succession.

So just put 1000 methods into your array.

查看更多
登录 后发表回答