I'm trying to generate an arraylist of random numbers and display it. I'm not sure where I'm going wrong. I think my showArray method isn't working properly because it's displaying two random numbers and then repeating the second one n-2 times.
private static ArrayList<Integer> RandomArray(int n)
{
ArrayList<Integer> arrayRandom = new ArrayList<Integer>(n);
for (int i=0; i<n; i++)
{
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
Integer r = rand.nextInt() % 256;
arrayRandom.add(r);
}
return arrayRandom;
}
private static void ShowArray(ArrayList<Integer> randomArray)
{
int n = randomArray.size();
ArrayList<Integer> showArray = new ArrayList<Integer>(n);
for (int i = 0; i<n; i++)
{
int r = randomArray.get(i);
showArray.add(r);
}
System.out.println(showArray);
}
public static void main(String args[])
{
ShowArray(RandomArray(5));
}
So for example this would produce an output of
[132, 152, 152, 152, 152]
Any help is much appreciated. Thanks in advance