You can use IntStream ints() or DoubleStream doubles() available as of java 8 in Random class. something like this will work, depends if you want double or ints etc.

Random random = new Random();

int[] array = random.ints(100000, 10,100000).toArray();

you can print the array and you'll get 100000 random integers.

Answer from Samir Ouldsaadi on Stack Overflow
Discussions

java - How to randomly pick an element from an array - Stack Overflow
I am looking for solution to pick number randomly from an integer array. For example I have an array new int[]{1,2,3}, how can I pick a number randomly? More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Generate 10 Random Integers storing them in an Array and then calling a Method to display the Array - Stack Overflow
so i need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to fill an array with random numbers from 0 to 99 using the class Math? - Stack Overflow
Conversion in Java looks like cast in C. ... Sign up to request clarification or add additional context in comments. ... And is called casting as well. 2014-09-10T14:47:36.197Z+00:00 ... You need to strictfp this. 2018-11-04T20:37:19.13Z+00:00 ... When you cast, cast type should be in brackets e.g. (cast type)value ... You need to strictfp this. 2018-11-04T20:37:09.233Z+00:00 ... package studing; public class Array ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 6, 2014
[Java] Trying to fill an array with unique random numbers.
So you need to fill an array with unique random numbers. You need an array. You need to loop through each position of the array. At each position, generate a random number Try it with only One loop. If you get stuck, feel free to let me know. More on reddit.com
๐ŸŒ r/learnprogramming
7
1
November 15, 2014
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ generate-a-random-array-of-integers-in-java
Generate a random array of integers in Java
September 12, 2023 - import java.util.Random; public class Example { public static void main(String[] args) { Random rd = new Random(); // creating Random object int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = rd.nextInt(); // storing random integers in an array System.out.println(arr[i]); // printing each array element } } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ how to fill an array with random numbers
How to Fill an Array With Random Numbers - Java
August 13, 2024 - Among the various available approaches, we can iteratively fill the content of an array with random numbers using the methods provided by the Random, SecureRandom, and ThreadLocalRandom classes, which are suitable for different scenarios.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ add-random-number-to-an-array-in-java
How to Add Random Number to an Array in Java? - GeeksforGeeks
July 23, 2025 - ... // Java Program to Add Random ... 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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ java-program-to-generate-a-random-number-from-an-array
Java program to generate a random number from an array
November 4, 2024 - Retrieve and print the random element. ... args) { int[] arr = new int[] { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; System.out.print("Random number from the array = "+arr[new Random().nextInt(arr.length)]); } }...
Find elsewhere
๐ŸŒ
CodingNConcepts
codingnconcepts.com โ€บ java โ€บ generate-random-numbers-in-java
How to generate Random Numbers in Java - Coding N Concepts
May 21, 2023 - Letโ€™s use the above method to generate an Array of 5 random numbers between 0 to 9 inclusive. The method will generate a random array every time you run it. System.out.println(Arrays.toString(generateRandomArray(5, 0, 9))); // Prints random array "[1, 9, 7, 0, 4]" // Prints random array "[7, 1, 2, 8, 5]" // Prints random array "[5, 7, 5, 2, 3]" Letโ€™s create a method using Random class and Java Streams to generate a List of random numbers of a given size and all elements in that List should be in the given range of min and max values inclusive.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-fill-an-array-with-random-numbers
Java Program to fill an array with random numbers
import java.util.Arrays; import java.util.Random; public class Demo { public static void main(String args[]) { double[] arr = new double[5]; Random randNum = new Random(); for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); } System.out.println("Random numbers = "+Arrays.toString(arr)); } } Random numbers = [-6.59888981E8, 1.141160731E9, -9.931249E8, 1.335266582E9, 8.27918412E8] Samual Sam ยท
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 688929 โ€บ java โ€บ Fill-array-random-numbers
Fill an array with random numbers (Beginning Java forum at Coderanch)
December 3, 2021 - To part which is difficult to me ... Any Ideas,please? ****Sorry,if my english isnt the best****** ... John, Welcome to CodeRanch! Java comes with a random number generator....
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 323367 โ€บ array-of-random-numbers
java - Array of random numbers ... [SOLVED] | DaniWeb
To give you an idea of what you ... int temp; int arr[] = new int[20]; for(int i = 0; i < 20; i++) { //random numbers from 1 to 10: temp = r.nextInt(10) + 1; // this will give you a random number between 0 to 10....
๐ŸŒ
OneCompiler
onecompiler.com โ€บ questions โ€บ 3xmsr4s6w โ€บ -java-how-to-create-an-array-of-random-numbers
[Java] How to create an array of random numbers? - Questions - OneCompiler
Following code shows how to generate array of length 10 with random numbers. import java.util.Arrays; import java.util.Random; import java.util.stream.IntStream; public class JavaArrays { public static void main(String[] args) { int arrayLength = 10; int maxNumber = 1000; int[] randomIntsArray = IntStream.generate(() -> new Random().nextInt(maxNumber)).limit(arrayLength).toArray(); System.out.println(Arrays.toString(randomIntsArray)); } }
๐ŸŒ
Chron.com
smallbusiness.chron.com โ€บ assign-random-numbers-array-java-29189.html
How to Assign Random Numbers to an Array in Java
August 12, 2022 - The following code instantiates the "RandomNumber" class and uses it to create a random number between one and 100:Random gen = new Random(); int randomNum= gen.nextInt(100); Add the random number to the array.
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ Java-generate-array-with-10-random-numbers-gprZAp
Java - generate array with 10 random numbers - Dirask
// generate random numbers between 60 and 100 int minVal = 60; int maxVal = 100; int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = ThreadLocalRandom.current().nextInt(minVal, maxVal); } // [98, 93, 80, 61, 64, 70, 73, 64, 71, 86] System.out.println(Arrays.toString(arr));
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] trying to fill an array with unique random numbers.
r/learnprogramming on Reddit: [Java] Trying to fill an array with unique random numbers.
November 15, 2014 -

Hello!

I'm learning to program at school now, and i have a problem i can't figure out. I'm trying to create an array and fill it with random numbers. But i want the numbers to be unique.

int[] list = new int[10];

list[0] = r.nextInt(20);

for(int i = 1; i < list.length; i++) {

 list[i] = r.nextInt(20);
 for(int j = 0; j < i; j++) {
      while(list[i] == list[j]) {
             list[i] = r.nextInt(10);
      }
 }

}

I can not figure out how to make this work and it is really bugging my mind. Does anybody have any tips?

Edit* Sorry for the messed up edit. first time posting code, so i don't know how to make it appear as code yet :)

Top answer
1 of 2
1

Because Random is random, it won't generate sequences like that. You have to actually make a sequence if that's what you want.

There's a bit of tricky math in the indexes, you should write these out by hand to see how it works.

    Integer deck = new Integer[2*NumberOfPairs];
    for (int i = 0; i < NumberOfPairs; i++) {
        deck[i*2] = i;
        deck[i*2+1] = i;
    }

Now you have a list of values that aren't random but exactly the sequence you want. Now if you want them to be in a random order you need to shuffle them, like a deck of cards.

List<Integer> deckList = new ArrayList<>( Arrays.asList( deck) );
Collections.shuffle( deckList );
int i = 0;
DeckOfCards = new int[2*NumberOfPairs];
for( Integer x : deckList )
   DeckOfCards[i++] = x;

Now you have some preset values in a random order. This would be a bit less complicated if you used an ArrayList for DeckOfCards instead of an plain int array. (Code is untested.)

(For comparison, I'll write the same code with DeckOfCards as an ArrayList<Integer>.)

DeckOfCards = new ArrayList<>();
for (int i = 0; i < NumberOfPairs; i++) {
   DeckOfCards.add( i );
   DeckOfCards.add( i );
}
Collections.shuffle( DeckOfCards );

(One more edit: if you are actually building a deck of cards, the usual way to do it is just to assign a List the numbers 0 through 51 (52 values for each card). Then a suit is numbers 0 through 3 (space, heart, diamond, club) like this card / 13 -- that's card divided by 13 and the face of each card is card % 13 where the face value of 10 or less are their own number+1, an ace is 0, and the values of jack, queen and king are 10, 11, and 12.)

2 of 2
0

The main thought of what you want to do is to get all the numbers inside an array and then suffle them.


Firstly, you must create an array with all the numbers that you want. In your case the numbers that you want are from 0 to NumberOfPairs two times. For example if the NumberOfPairs = 3, then the numbers that you have are (0, 0, 1, 1, 2, 2). Therefore, you got this code:

import java.util.Random;

public class Board {
    private int NumberOfCards;
    private int NumberOfPairs;
    private int[] DeckOfCards;
    private int CardsRemaining;
    
    public Board(int NumberOfPairs){
        this.NumberOfPairs = NumberOfPairs;
        this.NumberOfCards = NumberOfCards*2;
        this.CardsRemaining = CardsRemaining;
        DeckOfCards = new int [2*NumberOfPairs];
        Random numbers = new Random();

        for (int i = 0; i < NumberOfCards; i++) {
            for (int j = 0; j < 1; j++) {
                DeckOfCards[i] = i;
            }
        }
    }
}

And finally, you must suffle the numbers. To suffle the numbers, you have to switch every current position of the array with a random one. So, for this step your code is something like this:

import java.util.Random;

public class Board {
    private int NumberOfCards;
    private int NumberOfPairs;
    private int[] DeckOfCards;
    private int CardsRemaining;
    
    public Board(int NumberOfPairs){
        this.NumberOfPairs = NumberOfPairs;
        this.NumberOfCards = NumberOfPairs*2;
        this.CardsRemaining = CardsRemaining;
        DeckOfCards = new int [2*NumberOfPairs];
        private int temp;
        private int randomPos;

        Random numbers = new Random();
        for (int i = 0; i < NumberOfCards; i++) {
            for (int j = 0; j < 1; j++) {
                DeckOfCards[i] = i;
            }
        }
        
        for (int i = 0; i < NumberOfCards; i++) {
            randomPos = random.nextInt(NumberOfCards);
            temp = DeckOfCards[i];
            DeckOfCards[i] = DeckOfCards[randomPos];
            DeckOfCards[randomPos] = DeckOfCards[temp];                
        }
    }
}

And you are done. I hope that I helped you.

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 673266 โ€บ java โ€บ Random-numbers-array
Random numbers in a 2d array (Beginning Java forum at Coderanch)
so i would need to assing the random to a 2d array. like this? public static int[][] newRandom(int m, int n) { int[][] size = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n ; j++){ size[i][j] = rand.nextInt(10)*10+1; } } return size; } ... JavaRanch-FAQ HowToAskQuestionsOnJavaRanch ...