random BigDecimal - Java java.math

Java examples for java.math:BigDecimal

Description

random BigDecimal

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        int range = 2;
        System.out.println(random(range));
    }//  w  w  w .  j av  a2  s  .com

    public static BigDecimal random(int range) {
        BigDecimal max = new BigDecimal(range);
        BigDecimal randFromDouble = new BigDecimal(Math.random());
        BigDecimal actualRandomDec = randFromDouble.multiply(max);
        actualRandomDec = actualRandomDec
                .setScale(2, BigDecimal.ROUND_DOWN);
        return actualRandomDec;
    }
}

Related Tutorials