0

How do I make this program print out negative values along with positive values so there will be random numbers from -100 to 100?

public class RandomInts {

    /* 
     * This program makes 10 random integers and prints them out on the console window.
     */
    public static void main(String[] args) {
        int[] myArray;      
        myArray = new int[10];

        for( int i = 0; i < myArray.length; i++ ){
            myArray[i] = (int)(Math.random() * 100);
        } // End of first for loop.

        for(int i=0;i<10;i++){
            System.out.println("My array is " + myArray[i]);
        } // End of second for loop.

    } // End of the main.

} // End of the class RandomInts.
Tom
  • 16,842
  • 17
  • 45
  • 54

2 Answers2

0

you could try -

public class RandomInts {

/* 
 * This program makes 10 random integers and prints them out on the console window.
 */
public static void main(String[] args) {
    int[] myArray;      
    myArray = new int[10];

    for( int i = 0; i < myArray.length; i++ ){
        myArray[i] = (int)(Math.random() * 200) - 100;
    } // End of first for loop.

    for(int i=0;i<10;i++){
        System.out.println("My array is " + myArray[i]);
    } // End of second for loop.

} // End of the main.

} // End of the class RandomInts.

Pramod
  • 29
  • 3
0

How about myArray[i] = r.nextInt() % 101 where Random r = new Random()

David Soroko
  • 8,521
  • 2
  • 39
  • 51