How to Add Random Number to an Array in Java? Last Updated : 18 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To generate an array of integers with random values the nextInt() method from the java.util.Random class is used. From the random number generator sequence, this method returns the next random integer value.Assigning a Random Value to an ArrayWe can assign random values to an array by two approaches. If we do not pass any arguments to the nextInt() method, it will assign some arbitrary random value of the integer range in Java. If we pass some argument to the nextInt() method, it will generate numbers in the range between 0 to the argument passed.Example 1: In this, we assign random values to an array without any range or between the minimum value of integers to the maximum value of integers (i.e. not passing arguments). Java // Java Program to Add Random Number to an Array import java.util.Random; import java.util.Arrays; public class Main { public static void main(String[] args) { int n = 5; int[] arr = new int[n]; // Create a Random object Random random = new Random(); // Assign random values to the array for (int i = 0; i < n; i++) { // Generate a random integer // between INT_MIN and INT_MAX arr[i] = random.nextInt(); } // Print the array System.out.println(Arrays.toString(arr)); } } Output[-500572953, 583909042, -501070326, 290658844, 1861542031] Example 2: In this, we are assigning random values to array with some specific range (i.e. passing arguments) Java // Java Program to Add Random Number to an Array import java.util.Random; import java.util.*; public class Main { public static void main(String[] args) { int n = 5; int[] arr = new int[n]; // Create a Random object Random random = new Random(); // Assign random values to the array for (int i = 0; i < n; i++) { // Generate a random integer // between 0 and 99 arr[i] = random.nextInt(100); } // Print the array System.out.println(Arrays.toString(arr)); } } Output[19, 68, 84, 53, 26] Comment More infoAdvertise with us Next Article How to Add Random Number to an Array in Java? V vivekchaudharyy Follow Improve Article Tags : Java Java Programs Arrays Java-Arrays Java-Array-Programs Java Examples +2 More Practice Tags : ArraysJava Similar Reads Java Program To Jumble an Array Given an array of size N, and the task is to shuffle the elements of the array or any other permutation. Using Java think of stuffing the array by generating random sequences of the elements present in the array is possible. This Algorithm is called Fisher-Yates Shuffle Algorithm. FisherâYates shuff 2 min read Java Program to Guess a Random Number in a Range Write a program that generates a random number and asks the user to guess what the number is. If the userâs guess is higher than the random number, the program should display Too high, try again. If the userâs guess is lower than the random number, the program should display Too low, try again. The 2 min read How to Implement an Array with Constant-Time Random Access in Java? In Java, random access in arrays requires direct index-based addressing, it usually takes constant time, or O(1). Standard arrays in Java provide random access that is constant in time. Note: The term constant-time random access means accessing an element by index, it takes the same amount of time i 2 min read How to Generate Unique Positive Long Number in Java? Java provides different ways to generate random numbers, using some built-in methods and classes, but most of them do generate unique positive long numbers like java.util.Random class, Math.random method and ThreadLocalRandom class, and many others. But in most of these either with randomness unique 3 min read Java Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i 3 min read Like