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
[Java] Trying to fill an array with unique random numbers.
java - How to fill an array with random numbers from 0 to 99 using the class Math? - Stack Overflow
2d array filled with random numbers
Java fill an array with random with random numbers - Stack Overflow
Videos
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 :)
Hi I need some desperate help, in my hw im supposed to do a 2d array and fill it with random number to add them later but my issue is the array in itself because for the love of everything this is not working. I also tried the math.random but still get the same problem
The first time I got this when running the program [I@28a418fc[[I@28a418fc and i looked up how to solve it, it said to use arrays.deeptostring to change the hash codes into actual numbers and to not use println but now I get this [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]][[84, 121, 30, 67, 138], [102, 87, 58, 95, 140], [113, 64, 80, 109, 104], [39, 122, 100, 85, 40], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] an i havent found what exactly this is either.
This is what I have so far
import java.util.Arrays;
import java.util.Random;
public class ejercicio6 {
public static void main(String args[]) {
int matriz [][] = new int [6][5];
Random rand = new Random();
for (int i = 0; i < matriz.length - 1; i++){ //filas
for(int j = 0; j < matriz.length - 1; j++){ //columnas
matriz[i][j] = rand.nextInt(150 -10 + 1) + 10;
System.out.print(Arrays.deepToString(matriz)); //deepToString porque me salen los hashcodes [I@5305068a
}
System.out.println("");
}Hey! First time posting, I just need some guidance on how to complete this question (I'm just now getting into Java). Alright so this is what I have so far.
public int makeIntArray(int size, int low, int high)
{
int [] array1 = new int [size];
for(int i = 0; ???)
}Alright, where the "???" is, is where I'm stuck. I'm not sure if I'm even supposed to use a for loop to tackle this question. All the information for this question, the method is called makeIntArray and it has three int parameters, size, low, and high, I needed to create an array based on the size parameter (I did that), and now I have to populate the array with random numbers from low to high. I just want someone to help me tackle this question so in the future, I can handle this question with no help. Thanks!