import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Random;

/**
* Robert Sedgewick Algorithms in C++<br>
* Chapter 35 Random Numbers<br>
* Problem10<br>
* 計算機の1語のサイズより大きいMを使って乱数を生成する方法を述べよ。
*/
public class Chapter35RandomNumbersProblem10 {
public static void main(String[] args) {
Chapter35RandomNumbersProblem10 o = new Chapter35RandomNumbersProblem10();
o.run();
}

private void run() {
Random rng = new Random();

String M = "";
for (int i = 0; i < 100; i++) {
M += (int) (10 * rng.nextDouble());
}

BigDecimal m = new BigDecimal(M);
debug("M\n", m);
double d = rng.nextDouble();
debug("d", d);
BigDecimal res = m.multiply(new BigDecimal(d));
debug("m multiply d\n", res);

d = 0.1;

BigDecimal res2 = m.multiply(new BigDecimal(d));
debug("m multiply d\n", res2);
BigDecimal res3 = m.divide(new BigDecimal(1 / d));
debug("m divide 1/d\n", res3);
if (!res2.equals(res3)) {
throw new AssertionError("res2 is not equal to res3. I think the problem is BigDecimal.multiply(0.1).");
}
}

private static final boolean DEBUG = true;

private static final void debug(Object... o) {
if (DEBUG)
System.err.println(Arrays.deepToString(o));
}

private static final void message(Object... o) {
System.err.println(Arrays.deepToString(o));
}

}