I have an array that can hold 5 integers.In the for loop I use Math.random() to fill the array with random integer values between 0 and 10 that can be positive or negative. How can I receive the negative value? Someone recommended me to multiply by -1 the formula to fill out array with positive and negative values but when I do this all my values in the array are negative. I think the problem is in this line
int r = 0 + (int) (Math.random() * 10 *(-1));
This is the code:
public class Random
{
public static void main(String [] args)
{
int [] arr= new int[5];
for(int k=0; k<arr.length; k++)
{
int r = 0 + (int) (Math.random() * 10 *(-1));
arr[k] = r;
}
int j = 0;
while(i<arr.length) {
System.out.print(arr[i] + " ");
j++;
}
}
}
Now my output is -7 -3 -3 -5 -6
I want my output to be 7 -3 3 -5 6