title: How can I create a random BigDecimal in Java?
body: <p>This question: <a href="https://stackoverflow.com/questions/2290057/how-to-generate-a-random-biginteger-value-in-java">How to generate a random BigInteger</a> describes a way to achieve the same semantics as Random.nextInt(int n) for BigIntegers.</p>
  
  <p>I would like to do the same for BigDecimal and Random.nextDouble().</p>
  
  <p>One answer in the above question suggests creating a random BigInteger and then creating a BigDouble from it with a random scale. A very quick experiment shows this to be a very bad idea :)</p>
  
  <p><strike>My intuition is that using this method would require the integer to be scaled by something like <code>n-log10(R)</code>, where n is the number of digits of precision required in the output and R is the random BigInteger. This should allow the correct number of digits to be present so that (for example) 1 -> 10^-64 and 10^64 -> 1.</p>
  
  <p>The scaling value also needs to be chosen correctly for the result to fall in the range [0,1].</strike></p>
  
  <p>Has anyone done this before, and do they know if the results are correctly distributed? Is there a better way to achieve this?</p>
  
  <p><strong>EDIT:</strong> Thanks to @biziclop for correcting my understanding of the scale argument. The above isn't necessary, a constant scale factor has the desired effect.</p>
  
  <p>For later reference, my (apparently working code) is:</p>
  
  <pre><code>private static BigDecimal newRandomBigDecimal(Random r, int precision) {
      BigInteger n = BigInteger.TEN.pow(precision);
      return new BigDecimal(newRandomBigInteger(n, r), precision);
  }
  
  private static BigInteger newRandomBigInteger(BigInteger n, Random rnd) {
      BigInteger r;
      do {
          r = new BigInteger(n.bitLength(), rnd);
      } while (r.compareTo(n) &gt;= 0);
  
      return r;
  }
  </code></pre>