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