Ask Your Question
1

I was trying to design random Latin Square. Could you please tell me why this code doesn't work?

asked 2017-07-14 09:49:10 -0500

Dawn gravatar image

updated 2017-07-14 15:54:06 -0500

Code:

import sys
from sage.all import *
def rand_LS(n):
    m=LatinSquare(n)
    m[0,0]=randint(0,n-1)
    for i in range(0,n-1):
        for j in range(0,n-1):
            while true:
                m[i,j]=randint(0,n-1)
                if (m.is_completable()):
                    break
    return m
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-14 10:19:01 -0500

tmonteil gravatar image

range(0,n-1) goes from 0 to n-2, you should use range(0,n) or range(n).

edit flag offensive delete link more

Comments

But keep the randint(0, n-1) as it is! It is one of the confusing Python conventions.

vdelecroix gravatar imagevdelecroix ( 2017-07-14 16:00:54 -0500 )edit

Indeed, and you can replace randint(0, n-1) with randrange(n).

tmonteil gravatar imagetmonteil ( 2017-07-14 17:33:08 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-07-14 09:35:51 -0500

Seen: 86 times

Last updated: Jul 14