Java Tutorial
public class Random extends Object implements Serializable
protected int next(int bits)
import java.util.*; class JavaRandom3 extends Random{ public static void main(String args[]) { JavaRandom3 random = new JavaRandom3(); for(int i=1; i<=5; i++) { System.out.println("Random Integer" + i + ": " + random.next(8)); } } }Output:
$ java JavaRandom3 Random Integer1: 198 Random Integer2: 224 Random Integer3: 61 Random Integer4: 202 Random Integer5: 22Random.nextInt method - returns the next pseudorandom integer.
public int nextInt()
import java.util.*; class JavaRandom { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random Integer" + i + ": " + random.nextInt()); } } }output:
$ java JavaRandom Random Integer1: -1689153053 Random Integer2: -1691680029 Random Integer3: 1952187265 Random Integer4: 103298371 Random Integer5: -356456947
int nextInt(int limit)
import java.util.*; class JavaRandom1 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=10; i++) { System.out.println("Random Integer" + i + ": " + random.nextInt(10)); } } }Output:
$ java JavaRandom1 Random Integer1: 5 Random Integer2: 3 Random Integer3: 8 Random Integer4: 1 Random Integer5: 4 Random Integer6: 0 Random Integer7: 5 Random Integer8: 8 Random Integer9: 5 Random Integer10: 5
public long nextLong()
import java.util.*; class JavaRandom2 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random Long Integer" + i + ": " + random.nextLong()); } } }Output:
$ java JavaRandom2 Random Long Integer1: 6204425201330476624 Random Long Integer2: 3939077056305662022 Random Long Integer3: -6116547507993421664 Random Long Integer4: -5425591754091129004 Random Long Integer5: 733576223822010011
void setSeed(long seedValue)
import java.util.*; class JavaRandom2 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { if(i==5) { random.setSeed(100); } else { random.setSeed(7366551435105484086L); } System.out.println("Random Long Integer" + i + ": " + random.nextLong()); } } }Output:
$ java JavaRandom2 Random Long Integer1: -1302919629888628344 Random Long Integer2: -1302919629888628344 Random Long Integer3: -1302919629888628344 Random Long Integer4: -1302919629888628344 Random Long Integer5: -5128016860359238732
boolean nextBoolean()
import java.util.*; class JavaRandom4 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random " + i + ": " + random.nextBoolean()); } } }Output:
Random 1: false Random 2: false Random 3: false Random 4: true Random 5: true
void nextBytes(byte[] bytes)
import java.util.*; class JavaRandom5 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { byte[] bytes = new byte[100]; random.nextBytes(bytes); System.out.println("Random " + i + ": " + bytes); } } }Output:
$ java JavaRandom5 Random 1: [B@15db9742 Random 2: [B@6d06d69c Random 3: [B@7852e922 Random 4: [B@4e25154f Random 5: [B@70dea4e
float nextFloat()
import java.util.*; class JavaRandom6 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random Float" + i + ": " + random.nextFloat()); } } }Output:
$ java JavaRandom6 Random Float1: 0.074386775 Random Float2: 0.9043543 Random Float3: 0.48675168 Random Float4: 0.5216956 Random Float5: 0.037228405
double nextDouble()
import java.util.*; class JavaRandom7 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random double" + i + ": " + random.nextDouble()); } } }Output:
$ java JavaRandom7 Random double1: 0.1232464994256951 Random double2: 0.7140631066463907 Random double3: 0.9438924024885673 Random double4: 0.24870371110216516 Random double5: 0.5521996971170559
import java.util.*; class JavaRandom2 { public static void main(String args[]) { //Random random = new Random(); for(int i=1; i<=5; i++) { //System.out.println("Random double" + i + ": " + random.nextInt(10)); System.out.println("Random double" + i + ": " + Math.random()); } } }Output:
$ java JavaRandom2 Random double: 0.3276202477160206 Random double: 0.05003947696588251 Random double: 0.957158207424922 Random double: 0.053595606471153134 Random double: 0.030469717460436252
public double nextGaussian()
import java.util.*; class JavaRandom8 { public static void main(String args[]) { Random random = new Random(); for(int i=1; i<=5; i++) { System.out.println("Random double" + i + ": " + random.nextGaussian()); } } }Output:
$ java JavaRandom8 Random double1: -0.2384602405147781 Random double2: 1.2574930592267088 Random double3: -0.27873686337275844 Random double4: 1.7361407368198931 Random double5: 1.0019820158656176
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page