double型で指定の範囲からランダム

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

テスト

for (int i = 1 ; i <=5 ; i++){
    Random r = new Random();
    double randomValue = 123.004722 + (145.816278 - 123.004722) * r.nextDouble();
    System.out.println(randomValue);
}

結果

132.99636946543728
134.89191866434484
139.8226971022699
124.47873288915928
142.50470752520263

参考

https://stackoverflow.com/questions/3680637/generate-a-random-double-in-a-range

カテゴリー: のーと タグ: , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です