The middle-square method is a method of generating pseudorandom numbers. In practice, it's not a good method, since its period is usually very short and it has some severe weaknesses. The defects associated with the original middle-square generator can be rectified by running the middle square with a Weyl sequence, which prevents convergence to zero.
Write a function msws(seed, n) that can generate the n-th random 16-bit long number. The implementation should result in fast performance.
Algorithm description:
marsaglia = 362437
r, weyl = seed, 0
# repeat n times to get n-th random number r:
r = r ** 2
weyl = (weyl + marsaglia) % 2 ** 32
r = (r + weyl) % 2 ** 32
r = (r // 2 ** 8) % 2 ** 16

n (be inventive).