How can the use of arrays in PHP be optimized for generating and storing random numbers efficiently?

To optimize the use of arrays in PHP for generating and storing random numbers efficiently, we can preallocate the array size to avoid resizing during insertion of random numbers. This can improve performance by reducing the number of memory allocations and copying operations.

<?php
// Set the size of the array
$array_size = 1000;

// Initialize the array with a fixed size
$random_numbers = array_fill(0, $array_size, 0);

// Generate and store random numbers in the array
for ($i = 0; $i < $array_size; $i++) {
    $random_numbers[$i] = mt_rand(1, 1000);
}

// Print the array for verification
print_r($random_numbers);
?>