import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class MatrixUtils {
    public static int[][] generateMatrix(int rows, int cols) {
        Random rand = new Random();
        int[][] matrix = new int[rows][cols];
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                matrix[i][j] = rand.nextInt(1000);
        return matrix;
    }

    public static int[][] copyMatrix(int[][] matrix) {
        return Arrays.stream(matrix).map(int[]::clone).toArray(int[][]::new);
    }

    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < Math.min(matrix.length, 10); i++) {
            for (int j = 0; j < Math.min(matrix[i].length, 10); j++) {
                System.out.printf("%5d", matrix[i][j]);
            }
            System.out.println();
        }
    }

    public static void printMatrixToFile(int[][] matrix, BufferedWriter writer) throws IOException {
        int count = 0;
        for (int[] row : matrix) {
            for (int val : row) {
                if (count < 100) {
                    writer.write(String.format("%5d", val));
                    count++;
                } else
                    return;
            }
            writer.newLine();
        }
    }
}