Pseudo-random number generators use a seed to start with, and this seed can be controlled by Sage. For example:
sage: set_random_seed(42)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 5 -4 7]
[-5 6 -2]
[-4 3 -6]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -3 4]
[ 0 1 -1]
[ 0 -3 4]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[-2 -1 -9]
[ 1 0 5]
[ 1 -2 8]
Now if i try again, i got the same sequence, because i use the same seed:
sage: set_random_seed(42)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 5 -4 7]
[-5 6 -2]
[-4 3 -6]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -3 4]
[ 0 1 -1]
[ 0 -3 4]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[-2 -1 -9]
[ 1 0 5]
[ 1 -2 8]
For your workflow (waiting to find a nice matrix and pick only that one), you can do as follows, knowing that set_random_seed()
without arguments picks a random new seed:
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 3 2 -9]
[ 1 1 -2]
[ 2 2 -3]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -1 2]
[ 0 1 -5]
[-2 0 7]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 2 -1 9]
[ 1 0 5]
[ 0 -5 -4]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 0 -1]
[-1 -1 4]
[ 0 1 -4]
If you like this one, you can ask what is the current seed:
sage: s = initial_seed() ; s
326656179144558892533217995600680485655L
Then you can construct this matrix again:
sage: set_random_seed(326656179144558892533217995600680485655L)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 0 -1]
[-1 -1 4]
[ 0 1 -4]
EDIT: Actually, the sage_input
function does what you want out of the box:
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -2 4]
[ 0 1 -5]
[ 0 1 -4]
sage: sage_input(B)
matrix(ZZ, [[1, -2, 4], [0, 1, -5], [0, 1, -4]])