askvity

How to create an array of 100 random numbers Java?

Published in Java Array Random Numbers 3 mins read

Here's how to create an array of 100 random numbers in Java, along with explanations and an example based on the provided reference:

Approach

The basic approach involves the following steps:

  1. Declare and initialize an array of integers with a size of 100.
  2. Use a loop to iterate through each element of the array.
  3. Generate a random number within the desired range in each iteration.
  4. Assign the random number to the current element of the array.

Example Code and Explanation

import java.util.Random;

public class RandomArray {
    public static void main(String[] args) {
        // 1. Declare and initialize an array of 100 integers.
        int[] array = new int[100];

        // 2. Create a Random object to generate random numbers.
        Random random = new Random();

        // 3. Loop through the array and assign random numbers.
        for (int i = 0; i < 100; i++) {
            // Generate a random integer between 0 (inclusive) and 1000 (exclusive). You can adjust this as needed.
            int randomNumber = random.nextInt(1000);  //Generates numbers from 0-999

            // Assign the random number to the array element.
            array[i] = randomNumber;
        }

        // 4. Print the array (optional, for verification).
        System.out.println("Array of 100 Random Numbers:");
        for (int i = 0; i < 100; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}

Explanation

  • import java.util.Random;: This line imports the Random class, which is necessary for generating random numbers.

  • int[] array = new int[100];: This line declares an integer array named array and initializes it with a size of 100. This means the array can hold 100 integer values. This is point 5 in the referenced material.

  • Random random = new Random();: This line creates an instance of the Random class. You'll use this random object to generate your random numbers.

  • for (int i = 0; i < 100; i++) { ... }: This for loop iterates 100 times, once for each element in the array. This is point 6 in the referenced material.

  • int randomNumber = random.nextInt(1000);: This line generates a random integer between 0 (inclusive) and 1000 (exclusive). The nextInt(1000) method returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and 1000 (exclusive). You can change the 1000 to any other number to change the range of random numbers generated.

  • array[i] = randomNumber;: This line assigns the generated randomNumber to the i-th element of the array.

Customizing the Range

To generate random numbers within a specific range (e.g., between min and max inclusive), you can use the following formula:

int randomNumber = random.nextInt(max - min + 1) + min;

For example, to generate random numbers between 1 and 999 (inclusive):

int min = 1;
int max = 999;
int randomNumber = random.nextInt(max - min + 1) + min;

Important Notes

  • The Random class generates pseudorandom numbers. These are suitable for most general-purpose tasks.
  • For security-sensitive applications, consider using java.security.SecureRandom for generating cryptographically secure random numbers.

Related Articles