Write a Python code to generate an n-by-n magic square matrix, where the elements are distinct positive integers from 1 to n^2 and the sum of each columns, rows and 2 diagonals are the same. Provide your response in exactly 5 paragraphs (separated nearby paragraphs with 3 aterisk symbols ***).
user