added rand_mtx function and a mtx_to_nx function