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 OverflowYou 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.
You need to add logic to assign random values to double[] array using randomFill method.
Change
public static double[] list(){
anArray = new double[10];
return anArray;
}
To
public static double[] list() {
anArray = new double[10];
for(int i=0;i<anArray.length;i++)
{
anArray[i] = randomFill();
}
return anArray;
}
Then you can call methods, including list() and print() in main method to generate random double values and print the double[] array in console.
public static void main(String args[]) {
list();
print();
}
One result is as follows:
-2.89783865E8
1.605018025E9
-1.55668528E9
-1.589135498E9
-6.33159518E8
-1.038278095E9
-4.2632203E8
1.310182951E9
1.350639892E9
6.7543543E7
If you want to generate random integer array from an interval, here are the options
Copy// generate 100 random number between 0 to 100
int[] randomIntsArray = IntStream.generate(() -> new Random().nextInt(100)).limit(100).toArray();
//generate 100 random number between 100 to 200
int[] randomIntsArray = IntStream.generate(() -> new Random().nextInt(100) + 100).limit(100).toArray();
You can take input from the user by using a scanner like this -
CopyScanner input= new Scanner(System.in);
System.out.println("Enter the array size: ");
int n = input.nextInt();
Now create a function generateRandomArray(int n) like this -
Copypublic List<Integer> generateRandomArray(int n){
ArrayList<Integer> list = new ArrayList<Integer>(n);
Random random = new Random();
for (int i = 0; i < n; i++)
{
list.add(random.nextInt(1000));
}
return list;
}
Here - random.nextInt(1000) will generate a random number from the range 0 to 1000. You can fix the range as you want.
Now you can call the function with the value n get from the user -
CopyArrayList<Integer> list1 = generateRandomArray(n);
ArrayList<Integer> list2 = generateRandomArray(n);
Hope it will help.
java - How to randomly pick an element from an array - Stack Overflow
java - Generate 10 Random Integers storing them in an Array and then calling a Method to display the Array - Stack Overflow
java - How to fill an array with random numbers from 0 to 99 using the class Math? - Stack Overflow
[Java] Trying to fill an array with unique random numbers.
Videos
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
You can use the Random generator to generate a random index and return the element at that index:
//initialization
Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
return myArray[randomIndex];
You only have to use a single for loop - like this:
public static void main(String[] args)
{
int[] numbers = new int[10];
//Generates 10 Random Numbers in the range 1 -20
for(int i = 0; i < numbers.length; i++) {
numbers[i] = (int)(Math.random()*20 + 1);
}//end for loop
System.out.println("Numbers Generated: " + Arrays.toString(numbers));
}
To generate a random integer, you're best off using this:
Random rand = new Random();
numbers[i] = rand.nextInt(20)+1;
The rand.nextInt(20) call will give a random number from 0 to 19, so adding 1 makes it from 1 to 20.
In practice, you don't need to create a new Random() every time; you'd put this
Random rand = new Random();
at the beginning of your loop, and then
numbers[i] = rand.nextInt(20)+1;
inside it.
Since you've got several errors, I'd suggest you start again, write your code bit by bit, and check at each stage that it compiles and does what you want. For instance, start by printing a single random number out, and check that that works.
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 :)
Your for loop should be
for (int i = 0; i < r; i++)
This is one place I would definitely use streams. They have been around since Java 8 and everyone should be familiar with them. Even those new at coding in Java.
Random r = new Random();
int nElements = 10;
int maxElement = 50;
List<Integer> nums = r.ints(nElements, 1, maxElement+1).boxed().sorted()
.collect(Collectors.toList());
System.out.println(nums);
Here is the explanation.
- The first element to
r.intsis the quantity. The next two arestartandendingrange. Since the range is exclusive, 1 must be added to include themaxElementas a possibility. - Boxed() maps the ints to Integer objects to be collected into a List.
- Sorted() does what you would expect.
- And the collector puts them into a
List
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.)
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.