Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
import java.util.Random;
public class Demo {
private static final Random r = new Random();
public static void main(String[] args) {
int val = 5;
while (val<= 100) {
System.out.printf("%-4d", r.nextInt(20) * 1);
if (val % 5 == 0) {
System.out.println();
}
val++;
}
System.out.println();
val = 20;
while (val<= 100) {
System.out.printf("%-4d", r.nextInt(10 * (20)));
if (val % 5 == 0) {
System.out.println();
}
val++;
}
}
}
import java.util.Random;
public class Demo {
public static void main(String[] args) {
String[] strArr = { "P", "Q", "R", "S","T", "U", "V", "W" };
Random rand = new Random();
int res = rand.nextInt(strArr.length);
System.out.println("Displaying a random string = " + strArr[res]);
}
}
import java.math.BigInteger;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
BigInteger maxLimit = new BigInteger("5000000000000");
BigInteger minLimit = new BigInteger("25000000000");
BigInteger bigInteger = maxLimit.subtract(minLimit);
Random randNum = new Random();
int len = maxLimit.bitLength();
BigInteger res = new BigInteger(len, randNum);
if (res.compareTo(minLimit) < 0)
res = res.add(minLimit);
if (res.compareTo(bigInteger) >= 0)
res = res.mod(bigInteger).add(minLimit);
System.out.println("The random BigInteger = "+res);
}
}
import java.util.Random;
public class Demo {
public static void main(String[] args) {
for (int i = 0; i< 5; i++) {
Random rand = new Random();
if (rand.nextBoolean())
System.out.println(1);
else
System.out.println(-1);
}
}
}