RandomExpression.java


Below is the syntax highlighted version of RandomExpression.java from §2.3 Recursion.


/******************************************************************************
 *  Compilation:  javac RandomExpression.java
 *  Execution:    java RandomExpression p
 *
 *  %  java RandomExpression 0.25
 *  2
 *
 *  % java RandomExpression 0.25
 *  ((38 + (67 * (15 + 53))) + ((14 * (63 * (76 + (((1 + 78) + ((14 * 41) + (8 + 67))) + ((50 * (53 * (((39 * 19) * 24) + 88))) + 15))))) * 97))
 *
 *  % java RandomExpression 0.25
 *  ((56 * 28) * (52 + 52))
 *
 *  % java Expr 0.3
 *  Exception in thread "main" java.lang.StackOverflowError
 *          at java.lang.System.arraycopy(Native Method)
 *          at java.lang.String.getChars(String.java:726)
 *
 ******************************************************************************/


public class RandomExpression {

    public static String generate(double p) {
        double r = StdRandom.uniformDouble(0.0, 1.0);
        if      (r <= p)   return "(" + generate(p) + " + " + generate(p) + ")";
        else if (r <= 2*p) return "(" + generate(p) + " * " + generate(p) + ")";
        else               return "" + StdRandom.uniformInt(100);
    }

    public static void main(String[] args) {
        double p = Double.parseDouble(args[0]);
        StdOut.println(generate(p));
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:20:26 EDT 2022.