How to return a value by passing it through a random set of functions?

Teakon

I need help in figuring out how to generate a random set of functions.

Such as: Sin(Cos(Average(x, y)))

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

etc..

The code I've come up with thus far is as follows, but I'm pretty certain it's not correct:

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 ;



}

Any help or advice you can give me would be greatly appreciated. I know Math.sin & cos etc accept Radian values, but I'm thinking there is something wrong structurally.

Alex Kleiman

I would recommend using an array of functions, and then randomly choose functions from the array to call. To get you started, here is how you might declare your array:

// 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);}};

This will only work in Java 8. To do the same in Java 7, simply declare anonymous MathFunction classes like so:

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

Then, use these classes to create your array:

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

Putting it all together:

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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set the return value of multiple generic functions with FakeItEasy?

From Dev

Passing functions through classes

From Dev

How to pass an array through functions and return it back

From Java

Django: How to filter model objects after passing through functions?

From Dev

Passing a value through URL

From Dev

Go: Passing functions through channels

From Dev

Passing parameters through nested functions

From Dev

Python - Passing variables through functions

From Dev

How to iterate through a linkedlist and return a value?

From Dev

how to get random value in struts set tag

From Dev

How to set a variable to a random value with bash

From Dev

Python passing return values to functions

From Dev

How to handle functions return value in Python

From Dev

How to set a value with the return value of a stored procedure

From Dev

Calling dll functions, through JNA, How to handle address passing through pointers in java?

From Dev

How to set selected value in dropdown through javascript

From Dev

How to set selected value in dropdown through javascript

From Dev

Passing boolean value through HttpResponse

From Dev

Variable value not passing through loop

From Dev

Marklogic: How to return a set of items from a sequence randomly (random sample)?

From Dev

Set random negative number with Random Functions in Jmeter?

From Dev

How to use a value from a random value set only once

From Dev

How to call a function, passing the return value (possibly void) of a functor?

From Dev

Passing Variables Through Functions in C++

From Dev

c++ passing nullptr through multiple functions

From Dev

Passing variable through switch statement with functions

From Dev

VBA passing multiple parameters to SQL through functions

From Dev

Passing Variables Through JQuery Click Functions

From Dev

javascript passing arguments through functions and atribution with '='

Related Related

  1. 1

    How to set the return value of multiple generic functions with FakeItEasy?

  2. 2

    Passing functions through classes

  3. 3

    How to pass an array through functions and return it back

  4. 4

    Django: How to filter model objects after passing through functions?

  5. 5

    Passing a value through URL

  6. 6

    Go: Passing functions through channels

  7. 7

    Passing parameters through nested functions

  8. 8

    Python - Passing variables through functions

  9. 9

    How to iterate through a linkedlist and return a value?

  10. 10

    how to get random value in struts set tag

  11. 11

    How to set a variable to a random value with bash

  12. 12

    Python passing return values to functions

  13. 13

    How to handle functions return value in Python

  14. 14

    How to set a value with the return value of a stored procedure

  15. 15

    Calling dll functions, through JNA, How to handle address passing through pointers in java?

  16. 16

    How to set selected value in dropdown through javascript

  17. 17

    How to set selected value in dropdown through javascript

  18. 18

    Passing boolean value through HttpResponse

  19. 19

    Variable value not passing through loop

  20. 20

    Marklogic: How to return a set of items from a sequence randomly (random sample)?

  21. 21

    Set random negative number with Random Functions in Jmeter?

  22. 22

    How to use a value from a random value set only once

  23. 23

    How to call a function, passing the return value (possibly void) of a functor?

  24. 24

    Passing Variables Through Functions in C++

  25. 25

    c++ passing nullptr through multiple functions

  26. 26

    Passing variable through switch statement with functions

  27. 27

    VBA passing multiple parameters to SQL through functions

  28. 28

    Passing Variables Through JQuery Click Functions

  29. 29

    javascript passing arguments through functions and atribution with '='

HotTag

Archive