0

Possible Duplicate:
Generating random BigDecimal value from given range

How to create a lot of values BigDecimal randomly between 0 and 1 ?

Community
  • 1
  • 1
Mehdi
  • 2,160
  • 6
  • 36
  • 53

2 Answers2

0

Take a look at this JavaDoc. Once you have a numerical value for a lot, do a simple for loop and use the Random class to initialze random double values.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • `Random` return only `flot`, `long`, `double` ... but not `BigDecimal` – Mehdi May 10 '12 at 09:50
  • It is a pity we do not force people who downvote to leave some sort of comment... – npinti May 10 '12 at 09:57
  • @user15992 It sounds like it would benefit you to spend a few minutes browsing the javadocs for the BigDecimal class. BigDecimal has several constructors, some of which take doubles as parameters. – mcfinnigan May 10 '12 at 09:57
0

Try this code

class BigDecRand {
 public static void main(String[] args) {
    String range = args[0];
    BigDecimal max = new BigDecimal(range + ".0");
    BigDecimal randFromDouble = new BigDecimal(Math.random());
    BigDecimal actualRandomDec = randFromDouble.divide(max,BigDecimal.ROUND_DOWN);

    BigInteger actualRandom = actualRandomDec.toBigInteger();
 }

}

for more details

kannappan
  • 2,250
  • 3
  • 25
  • 35
  • 1
    Copying code from a similar question and pretend it to be yours is perhaps not very polite? Except for that, if the poster really wants a BigDecimal, I would assume that he needs higher precision that what this answer will give him. – jarnbjo May 10 '12 at 09:57