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 16:49:10 +0200

Dawn gravatar image

updated 2017-07-14 22:54:06 +0200

vdelecroix gravatar image

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 17:19:01 +0200

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 23:00:54 +0200 )edit

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

tmonteil gravatar imagetmonteil ( 2017-07-15 00:33:08 +0200 )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 16:35:51 +0200

Seen: 314 times

Last updated: Jul 14 '17