logo

Java Rastgele sınıfı

Java Random sınıfı, sözde rastgele sayılardan oluşan bir akış oluşturmak için kullanılır. Random sınıfı tarafından uygulanan algoritmalar, her çağrıda 32'ye kadar sözde rastgele oluşturulmuş bit sağlayabilen korumalı bir yardımcı program yöntemi kullanır.

Yöntemler

Yöntemler Tanım
çiftler() Sınırsız sayıda sahte rastgele çift değer akışı döndürür.
ints() Sınırsız sayıda sahte rastgele int değerleri akışı döndürür.
uzunlar() Sınırsız sayıda sahte rastgele uzun değer akışı döndürür.
Sonraki() Bir sonraki sözde rasgele sayıyı üretir.
sonrakiBoolean() Rastgele sayı üretecinin dizisinden bir sonraki düzgün dağıtılmış sözde rasgele boole değerini döndürür
nextByte() Rastgele baytlar üretir ve bunları belirtilen bayt dizisine yerleştirir.
nextDouble() Rastgele sayı üretecinin dizisinden 0,0 ile 1,0 arasındaki bir sonraki sözde rastgele Double değerini döndürür
nextFloat() Bu rastgele sayı üretecinin dizisinden 0,0 ile 1,0 arasında bir sonraki düzgün dağıtılmış sözde rastgele Float değerini döndürür
sonrakiGaussian() Bu rastgele sayı üretecinin dizisinden ortalama 0,0 ve standart sapma 1,0 olan bir sonraki sözde rastgele Gauss çift değerini döndürür.
nextInt() Bu rasgele sayı üretecinin dizisinden oluşturulan, eşit dağılımlı bir sözde rasgele int değeri döndürür
sonrakiUzun() Rastgele sayı üretecinin dizisinden bir sonraki düzgün dağıtılmış sözde rasgele uzun değeri döndürür.
setSeed() Bu rastgele sayı üretecinin tohumunu tek bir uzun tohum kullanarak ayarlar.

örnek 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;>

Örnek 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); } } 
Şimdi Test Edin

Çıktı:

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