Tutorialspoint

How can I generate two separate outputs using Random in Java

Samual Sam
Answered 8h 48m ago

To generate two separate outputs, at first create a new Random object −private static final Random r = new Random();Now, let us declare a value −int val = 5;Loop from the value till 100 and generate random numbers between 1 to 100 −while (val<= 100) {    System.out.printf("%-4d", r.nextInt(20) * ... Read More

Generate a random string in Java

karthikeya Boyini
Answered 11h 7m ago

Let us first declare a string array and initialize −String[] strArr = { "P", "Q", "R", "S", "T", "U", "V", "W" };Now, create a Random object −Random rand = new Random();Generate random string −int res = rand.nextInt(strArr.length);Example Live Demoimport java.util.Random; public class Demo {    public static void main(String[] args) { ... Read More

How to generate random values that won’t repeat in Java

Samual Sam
Answered 11h 9m ago

To generate random values that won’t repeat, use HashSet collection. Firstly, create a random object and HashSet −Random randNum = new Random(); Set<Integer>s = new HashSet<Integer>();Now, add random integers −while (s.size() < 10) {    s.add(randNum.nextInt()); }Now, display the random numbers that are unique −List<Integer>list = new ArrayList<Integer>(s); System.out.println(list);Example Live Demoimport ... Read More

Java Program to generate random numbers string

karthikeya Boyini
Answered 11h 11m ago

At first, create a character array −static char num[] = { '0', '1', '2', '3', '4', '5' };Now, let’s say you want to a string with length. Create a StringBuilder and use append() to create random numbers string out of it −int len = 5; StringBuilder strBuilder = new StringBuilder(); ... Read More

How to convert Integer array list to float array in Java?

Samual Sam
Answered 11h 13m ago

To convert integer array list to float array, let us first create an integer array list −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, convert integer array list to float array. We have first set the size to the ... Read More

Java Program to generate a random number from an array

karthikeya Boyini
Answered 11h 14m ago

To generate a random number, create a Random object and use nextInt(). The same works for array as well.Let us first create an array and add elements −int[] arr = new int[] { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200};Now, get a random number from array by ... Read More

How to generate a random BigInteger value in Java?

Samual Sam
Answered 11h 16m ago

To generate random BigInteger in Java, let us first set a min and max value −BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000");Now, subtract the min and max −BigInteger bigInteger = maxLimit.subtract(minLimit); Declare a Random object and find the length of the maxLimit: Random randNum = new Random(); ... Read More

Java Program to generate custom random number -1 or 1

karthikeya Boyini
Answered 11h 18m ago

To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE ... Read More

Generate 10 random four-digit numbers in Java

Samual Sam
Answered 11h 19m ago

To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to ... Read More

Java Program to append all elements of other Collection to ArrayList

karthikeya Boyini
Answered 11h 21m ago

Let us first create an ArrayList −ArrayList<String>arr = new ArrayList<String>(); arr.add("50"); arr.add("100"); arr.add("150"); arr.add("200"); arr.add("250"); arr.add("300");Now, let’s say we have another Collection as Vector −Vector<String>vector = new Vector<String>(); vector.add("500"); vector.add("700"); vector.add("800"); vector.add("1000"); Append all the elements from the Vector to ArrayList: arr.addAll(vector);Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo { ... Read More

Insert an element to List using ListIterator in Java

Samual Sam
Answered 11h 22m ago

Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class ... Read More

How to sort List in descending order using Comparator in Java

karthikeya Boyini
Answered 11h 24m ago

Let us first create an ArrayList −ArrayList<Integer>arrList = new ArrayList<Integer>(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo {    public static void main(String[] args) ... Read More

Java Program to convert LocalDate to java.util.Date

Samual Sam
Answered 11h 26m ago

Let us first set the LocalDate to current date −LocalDate date = LocalDate.now();Create an Instant −Instant i = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();Convert it to java.util.Date −java.util.Date.from(i)Example Live Demoimport java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       ... Read More

Java Program to convert LocalDateTime to java.util.Date

karthikeya Boyini
Answered 11h 29m ago

Set the LocalDateTime to current datetime −LocalDateTime dateTime = LocalDateTime.now();Create an Instant −Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant(); Convert LocalDateTime to java.util.Date: java.util.Date date = Date.from(i);Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.now(); ... Read More

Java Program to divide a number into smaller random ints

Samual Sam
Answered 11h 31m ago

We have considered a number 10 here, which will divided into 8 random ints with Random class. The number we have set as HashSet collection −HashSet<Integer>set = new HashSet<Integer>(); set.add(0); set.add(0); set.add(0); set.add(number);Now use nextInt to get the next random integer −intarrSize = parts + 1; while (set.size() < arrSize) ... Read More

Advertisements
Loading...
Unanswered Questions View All

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.