Javaランダムクラス

Java Random クラスは、擬似乱数のストリームを生成するために使用されます。 Random クラスによって実装されたアルゴリズムは、呼び出しごとに最大 32 個の疑似ランダムに生成されたビットを提供できる保護されたユーティリティ メソッドを使用します。

メソッド

メソッド 説明
ダブルス() 擬似ランダム double 値の無制限のストリームを返します。
ints() 擬似乱数の int 値の無制限のストリームを返します。
ロングス() 擬似乱数のlong値の無制限のストリームを返します。
次() 次の擬似乱数を生成します。
nextBoolean() 乱数生成器のシーケンスから次の一様に分布した擬似乱数のブール値を返します。
nextByte() ランダムなバイトを生成し、指定されたバイト配列に入れます。
nextDouble() 乱数ジェネレータのシーケンスから、0.0 ~ 1.0 の次の擬似乱数 Double 値を返します。
nextFloat() この乱数ジェネレータのシーケンスから、0.0 ~ 1.0 の次の一様分布疑似乱数 Float 値を返します。
nextガウス() この乱数発生器のシーケンスから、平均 0.0、標準偏差 1.0 の次の擬似乱数ガウス double 値を返します。
nextInt() この乱数ジェネレーターのシーケンスから生成された一様に分散された擬似乱数 int 値を返します。
nextLong() 乱数発生器のシーケンスから次の一様に分散された擬似乱数の Long 値を返します。
setSeed() 単一の長いシードを使用して、この乱数ジェネレーターのシードを設定します。

例1

 import java.util.Random; public class JavaRandomExample1 { public static void main(String[] args) { //create random object Random random= new Random(); //returns unlimited stream of pseudorandom long values System.out.println(&apos;Longs value : &apos;+random.longs()); // Returns the next pseudorandom boolean value boolean val = random.nextBoolean(); System.out.println(&apos;Random boolean value : &apos;+val); byte[] bytes = new byte[10]; //generates random bytes and put them in an array random.nextBytes(bytes); System.out.print(&apos;Random bytes = ( &apos;); for(int i = 0; i <bytes.length; i++) { system.out.printf('%d ', bytes[i]); } system.out.print(')'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Longs value : java.util.stream.LongPipeline$Head@14ae5a5 Random boolean value : true Random bytes = ( 57 77 8 67 -122 -71 -79 -62 53 19 ) </pre> <h2>Example 2</h2> <pre> import java.util.Random; public class JavaRandomExample2 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value System.out.println(&apos;Random Integer value : &apos;+random.nextInt()); // setting seed long seed =20; random.setSeed(seed); //value after setting seed System.out.println(&apos;Seed value : &apos;+random.nextInt()); //return the next pseudorandom long value Long val = random.nextLong(); System.out.println(&apos;Random Long value : &apos;+val); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Random Integer value : 1294094433 Seed value : -1150867590 Random Long value : -7322354119883315205 </pre></bytes.length;> 

例 2

 import java.util.Random; public class JavaRandomExample2 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value System.out.println(&apos;Random Integer value : &apos;+random.nextInt()); // setting seed long seed =20; random.setSeed(seed); //value after setting seed System.out.println(&apos;Seed value : &apos;+random.nextInt()); //return the next pseudorandom long value Long val = random.nextLong(); System.out.println(&apos;Random Long value : &apos;+val); } }  
今すぐテストしてください

出力:

 Random Integer value : 1294094433 Seed value : -1150867590 Random Long value : -7322354119883315205  

トップ記事

カテゴリ

興味深い記事