3

I am trying to add random values to an array. The user has to say how many values (length) and give the minval and maxval (e.x. minval = 3 means no values under 3). This is what I've got:

int[] GetallenArray;

public IntegerArray(int length, int maxval, int minval) {

    this.GetallenArray = new int[length];
    for (int i = 0; i < GetallenArray.length; i++)
    {
        this.GetallenArray[i] = // Random values between the maxval and minval
    }
}
MChaker
  • 2,610
  • 2
  • 22
  • 38
Mathias Verhoeven
  • 927
  • 2
  • 10
  • 24
  • This question has already been answered on http://stackoverflow.com/a/3321685/832748. – Paulo Miguel Almeida May 16 '15 at 17:05
  • Are you asking us to finish your code for us, or is there an issue with your code? – Ungeheuer May 16 '15 at 17:08
  • also, java convention states that objects like your array should generally start with a lower case letter, then the first 'a' would be capitalized like so `int[] getAllenArray;` It simply improves readability by adhering to a standardized model of writing your code. Works wonders when someone else reads your code. – Ungeheuer May 16 '15 at 17:11

2 Answers2

2

Instantiate a Random object outside for loop

Random random = new Random();

Then inside for loop

this.GetallenArray[i] = random.nextInt((maxval - minval)+1) + minval;
MChaker
  • 2,610
  • 2
  • 22
  • 38
1
Random random = new Random();
this.GetallenArray = new int[length];
  for (int i = 0; i < GetallenArray.length; i++){
    this.GetallenArray[i] = random.nextInt(50) + 1; 
   //50 is the maximum and the 1 is our minimum 
  }
}
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78