1. 程式人生 > >java 使用Random和Math random 去生成10以內的隨機數

java 使用Random和Math random 去生成10以內的隨機數

               
import java.util.Random;/** * Created by kdoulf on 2017/4/6. */public class RandomTest {    public static void main(String[] args){        //10以內的隨機數可以使用兩種方法,一個是Random一個是Math.random()*10,Math.random是0-1的隨機數        //其實Math.random 內部也是類似的呼叫的Randome randome = new Random().        //private static final class RandomNumberGeneratorHolder {
        //    static final Random randomNumberGenerator = new Random();        //}        //10以內的隨機數,double的        for (int i = 0; i < 10; i++) {            double v = Math.random() * 10;            System.out.println(v);            System.out.println(Math.ceil(v));        }        //10以內的隨機數        Random random = new
Random();        for (int i = 0; i < 10; i++) {            int i1 = random.nextInt(9)+1;            System.out.println(i1);        }    }}