Javaでランダムな整数を生成する方法
1. java.util.Random
この Random().nextInt(intbound) は、0 (両端を含む) から境界 (両端を含まない) までのランダムな整数を生成します。
(1) コード スニペット
getRandomNumberInRange(5, 10) の場合、5 (両端を含む) から 10 (両端を含む) までのランダムな整数が生成されます。
private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; }
(2) (max – min) とは 1) min?
上記の式は、 min (両端を含む) と max (両端を含む) の間のランダムな整数を生成します。
//Random().nextInt(int bound) = Random integer from 0 (inclusive) to bound (exclusive) //1. nextInt(range) = nextInt(max - min) new Random().nextInt(5); // [0...4] [min = 0, max = 4] new Random().nextInt(6); // [0...5] new Random().nextInt(7); // [0...6] new Random().nextInt(8); // [0...7] new Random().nextInt(9); // [0...8] new Random().nextInt(10); // [0...9] new Random().nextInt(11); // [0...10] //2. To include the last value (max value) = (range + 1) new Random().nextInt(5 + 1) // [0...5] [min = 0, max = 5] new Random().nextInt(6 + 1) // [0...6] new Random().nextInt(7 + 1) // [0...7] new Random().nextInt(8 + 1) // [0...8] new Random().nextInt(9 + 1) // [0...9] new Random().nextInt(10 + 1) // [0...10] new Random().nextInt(11 + 1) // [0...11] //3. To define a start value (min value) in a range, // For example, the range should start from 10 = (range + 1) + min new Random().nextInt(5 + 1) + 10 // [0...5] + 10 = [10...15] new Random().nextInt(6 + 1) + 10 // [0...6] + 10 = [10...16] new Random().nextInt(7 + 1) + 10 // [0...7] + 10 = [10...17] new Random().nextInt(8 + 1) + 10 // [0...8] + 10 = [10...18] new Random().nextInt(9 + 1) + 10 // [0...9] + 10 = [10...19] new Random().nextInt(10 + 1) + 10 // [0...10] + 10 = [10...20] new Random().nextInt(11 + 1) + 10 // [0...11] + 10 = [10...21] // Range = (max - min) // So, the final formula is ((max - min) + 1) + min //4. Test [10...30] // min = 10 , max = 30, range = (max - min) new Random().nextInt((max - min) + 1) + min new Random().nextInt((30 - 10) + 1) + 10 new Random().nextInt((20) + 1) + 10 new Random().nextInt(21) + 10 //[0...20] + 10 = [10...30] //5. Test [15...99] // min = 15 , max = 99, range = (max - min) new Random().nextInt((max - min) + 1) + min new Random().nextInt((99 - 15) + 1) + 15 new Random().nextInt((84) + 1) + 15 new Random().nextInt(85) + 15 //[0...84] + 15 = [15...99] //Done, understand?
(3) 5 (両端を含む) から 10 (両端を含む) までの範囲の 10 個のランダムな整数の完全な例を生成します。
package com.mkyong.example.test; import java.util.Random; public class TestRandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(getRandomNumberInRange(5, 10)); } } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } }
出力
7
6
10
8
9
5
7
10
8
5
2. Math.Random
この Math.random() は、 0.0 (両端を含む) から 1.0 (両端を含まない) までのランダムな double 値を与えます。
(1) コード スニペット。 1.2 を参照すると、ほぼ同じ式です。
(int)(Math.random() * ((max - min) 1)) min
(2) は 16 (両端を含む) から 20 (両端を含む) を生成します範囲内の 10 個のランダムな整数の完全な例)。
package com.mkyong.example.test; public class TestRandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(getRandomNumberInRange(16, 20)); } } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } return (int)(Math.random() * ((max - min) + 1)) + min; } }
出力
17
16
20
19
20
20
20
17
20
16
3. Java 8 Random.ints
Java 8 では、新しいメソッド java.util.Random
public IntStream ints(int randomNumberOrigin, int randomNumberBound) public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
This Random.ints( int Origin, intbound) または Random.ints(int min, int max) は、原点 (両端を含む) から境界 (両端を含まない) までのランダムな整数を生成します。
(1) コード スニペット。
private static int getRandomNumberInRange(int min, int max) { Random r = new Random(); return r.ints(min, (max + 1)).findFirst().getAsInt(); }
(2) 33 (両端を含む) ~ 38 (両端を含む) の範囲の 10 個のランダムな整数の完全な例を生成します。
package com.mkyong.form.test; import java.util.Random; public class TestRandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(getRandomNumberInRange(33, 38)); } } private static int getRandomNumberInRange(int min, int max) { Random r = new Random(); return r.ints(min, (max + 1)).limit(1).findFirst().getAsInt(); } }
出力
34
35
37
33
38
37
34
35
36
37
(3) ご参考までに、おまけ。
ストリーム サイズ 10 で、33 (両端を含む) ~ 38 (両端を含まない) の範囲のランダムな整数を生成します。 forEach.
//Java 8 only new Random().ints(10, 33, 38).forEach(System.out::println);
34
37
37
34
34
35
36# で出力を出力します。 ##33
37
34
以上がJavaでランダムな整数を生成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

Java は、初心者と経験豊富な開発者の両方が学習できる人気のあるプログラミング言語です。このチュートリアルは基本的な概念から始まり、高度なトピックに進みます。 Java Development Kit をインストールしたら、簡単な「Hello, World!」プログラムを作成してプログラミングを練習できます。コードを理解したら、コマンド プロンプトを使用してプログラムをコンパイルして実行すると、コンソールに「Hello, World!」と出力されます。 Java の学習はプログラミングの旅の始まりであり、習熟が深まるにつれて、より複雑なアプリケーションを作成できるようになります。
