如何通过传递一组随机函数来返回值?

时间:2014-08-29 16:52:12

标签: java recursion random

我需要帮助来弄清楚如何生成一组随机函数。

例如:Sin(Cos(Average(x, y)))

OR:Cos(Tan(Sin(Sin(x)))

等。

到目前为止,我提出的代码如下,但我确定它不正确:

public class Functions {
public static double randomFunctions(int maxDepth, double x, double y) {
    int random ;
    double value = 1.0 ;
    if (maxDepth == 0) {
        random = (int) (Math.random() * 5 + 1);
        if (random == 1 || random == 2) {

            return x ;
        }
        if (random == 3 || random == 4) {
            return y ;
        }
        if (random == 5) {
            return Math.random() * 2 - 1 ;
        }
    } else if (maxDepth != 0) {
        random = (int) (Math.random() * 17 + 1) ;
        if (random == 1 || random == 2 || random == 3) {
            return Math.sin(Math.PI * randomFunctions(maxDepth-1, x, y)) ;
        }
        if (random == 4 || random == 5 || random == 6) {
            return Math.cos(Math.PI * randomFunctions(maxDepth-1, x, y)) ;
        }
        if (random == 7 || random == 8 || random == 9) {
            return (randomFunctions(maxDepth-1, x, y) + randomFunctions(maxDepth-1, x, y))/2.0 ;
        }
        if (random == 10 || random == 11 || random == 12) {
            return (randomFunctions(maxDepth-1, x, y) + randomFunctions(maxDepth-1, x, y))/2.0 ;
        }
        if (random == 13 || random == 14) {
            System.out.println(random);
            return x  ;

        }
        if (random == 15 || random == 16) {
            return y ;
        }
        else {
            return (Math.random() * 2 - 1);
        }

    }
    System.out.println("Fail");
    return 0.0 ;



}

您可以给我的任何帮助或建议将不胜感激。我知道Math.sin& cos等接受Radian值,但我认为结构上存在错误。

2 个答案:

答案 0 :(得分:0)

我建议使用一组函数,然后从数组中随机选择要调用的函数。为了帮助您入门,您可以通过以下方式声明阵列:

// define a functional interface which we will use
public Interface MathFunction {
    public double apply(double d);
}   

// declare our array of functions, using three lambda functions to statically declare
// the array
MathFunction[] functions = {x -> {return Math.cos(x);}, 
                            x -> {return Math.sin(x);},
                            x -> {return Math.tan(x);}};

这只适用于Java 8.要在Java 7中执行相同操作,只需声明匿名MathFunction类,如下所示:

class Cosine implements MathFunction {
    @Override
    public double apply(double d) {
        return Math.cos(d);
    }
}

然后,使用这些类来创建数组:

MathFunction[] functions = {new Cosine(), new Sine(), new Tangent()}; 

全部放在一起:

public static double randomFunctions(int maxDepth, double x, double y) {
    MathFunction[] functions = {new Cosine(), new Sine(), new Tangent()}; 

    int currX = x;
    int currY = y;

    // repeatedly apply functions to each value randomly
    for (int i = 0; i < maxDepth; i++) {
        Random r = new Random();
        currX = r.nextInt(functions.length - 1);
        currY = r.nextInt(functions.length - 1);
    }

    // randomly return one of the two numbers
    Random r = newRandom();
    if (r.nextBoolean()) {
        return currX;
    } else {
        return currY;
    }
}

答案 1 :(得分:0)

  

我正在使用Java 7

您可以拥有一个界面:

interface Function {
    double apply(double x, double y);
}

现在:

final Function avg = new Function() {
    @Override
    public double apply(double x, double y) {
        return (x + y)/2;
    }
};

final Function sin = new Function() {
    @Override
    public double apply(double x, double y) {
        return Math.sin(x);
    }
};

final Function cos = new Function() {
    @Override
    public double apply(double x, double y) {
        return Math.cos(x);
    }
};

...

您可以将sincosavg等添加到列表中,随机播放,然后应用第一个maxDepth功能。