Skip to main content

ABOUT THE RANDOM LATIN SQUARE GENERATOR MODULE

Purpose

The “rlsmoc.py” module implements the fastest known direct generator method for constructing highly random Latin Squares.

What is a “Latin Square”?

For a given size parameter N, a Latin Square is a grid of NxN cells, arranged in N rows and N columns, in which N symbols are assigned to the cells in such a manner that each symbol appears exactly once in every row and exactly once in every column. The Random Latin Square generator uses the numerical values 0 through N-1 as its symbol set, consistent with indexing conventions of the Python language (and many others).

What is a “Random Latin Square”?

It should be clear that the rigid “Latin Constraint” rules about “no symbol can appear twice in any row or in any column” result in a layout that is very non-random. In the context of Latin Squares, however, “randomness” refers to the manner in which Latin Squares are selected (“generated”). If a generator algorithm produces Latin Square layouts such that every possible Latin Square of the given size is equally likely to be delivered, then the generator algorithm is said to be “unbiased” and the set of Latin Squares produced is said to be “random.” Sets of Latin Squares that are not truly random, but very difficult to distinguish from truly random ones, are typically good enough for most practical purposes.

How many Latin Squares are there?

Lots, a vast number, even in the cases when N is relatively small. However, if you compare that number to the number of possible ways that the same set of symbols could be randomly placed into the square grid, the Latin Squares are a miniscule subset.

How are random Latin Squares typically generated?

The traditional method-of-choice has been the Jacobson-Matthews scrambler. To use this idea, you start with a Latin Square, and apply a very large (but unspecified) number of randomized scrambling “moves” to shuffle the symbols around the layout. The starting Latin Square is typically a special nonrandom case that is simple to compute. The scrambling approach requires a relatively large amount of effort to sufficiently break up patterns present in the starting layout. With enough scrambling, the final results are biased only to a small degree. The residual bias is almost undetectable for Latin Squares of size 10x10 and larger.

What does this Random Latin Square generator do differently?

It is a direct method. It starts from an empty layout, and attempts to place copies of every symbol as randomly as possible without violating the “Latin Constraint” rules. After each step, the number of empty cells is reduced by one. If it turns out that some bad randomized assignment choices are made somewhere in the process, a heuristic “conflict resolution” operation is applied to remove the temporary impasse while introducing as little bias as possible. A detectable bias remains for small Latin Squares, but less than the residual bias produced by the scrambling methods.

Does the Random Latin Square Generator have other strange properties?

Yes, but not necessarily bad. It tends to run about 3 times faster than a good implementation of a scrambling algorithm. It can generate Latin Squares for any size N=1 or greater (even when it is not a smart idea). It can be implemented in a manner that has guaranteed finite termination, unlike the scramblers where you must make a judgment call about how much mixing is “good enough.” This implementation, however, sacrifices some theoretical guarantees for the sake of faster execution.

Does the implementation have unusual code dependencies?

The random Latin Square generator uses the numpy module for its highly-structured array types with efficient indexing. Approximately 5 x N-squared storage locations are used for the Latin Square storage and internal indexing work areas. The “short” integer data type is used to keep data storage compact. This constrains the size of the Latin Squares to N < 32768, but this is very unlikely to be a practical limitation. Features of the numpy random number generator are used for making randomized choices.

Where can I find more information about the algorithm?

Glad you are interested. A detailed explanation is provided in the rlsmoc.epub (“Random Least Squares by the Method of Covers”) document provided with this module. It is tedious, but intended to be understandable. Don’t be intimidated.

Is the method published in the refereed journals?

No. It has generated a remarkable lack of interest from the academic community. Strange, because as the great Nikolai Lobachevsky once observed, the only thing that really matters is who publishes first. And this “unpublished” method is low-hanging fruit that anyone hungry to publish could easily exploit.

Why is this implementation considered “experimental?”

Not because it doesn’t work, or isn’t reliable… There are two main reasons. One, it is coded for clarity of presentation rather than optimality for any particular purpose: security, robustness to user error, ease of application, etc.. Two, it does not have the years of field application experience that you would want for a thoroughly trusted method. It is expected that you will adapt/constrain/improve/test the coding thoroughly before applying it. Various “optimizations” for specific purposes are left to you.

How do I use the Random Latin Square generator?

Easy. Import the module. Instantiate a Latin Square object of the desired size, which reserves storage. Apply the “fill” method to populate the Latin Square with the symbol assignments. Apply the “fill” method again to repopulate the Latin Square storage with new and different symbol assignments. See the “example.py” code file.

How do I access the results?

One of the arrays within the Random Latin Square object is a two dimensional array called “store” that you can access. The standard Python/numpy modules provide a simple “print” command that works fine for displaying small Latin Square results from that storage. For larger Latin Squares, you will probably need to develop some alternate storage and presentation methods, for example, copy the results into a file or database. Keep in mind that you can use the row indices, column indices, and grid contents as index numbers. For example, you could use the row index to access a list of “laboratory names”, the column index to access a list of “test dates”, and the cell contents to index a list of “trial codes,” to organize and display a Latin Square design for a pharmaceutical safety test based on a Latin Square experiment design.

Are there any other obscure details?

Just one. Some remedial operations (somewhat reminiscent of scrambling operations used by other methods but on a greatly reduced scale) are applied by default to reduce bias. This has a measurable and perhaps relevant effect on small Latin Squares, but it is pointless for moderate to large Latin Squares. You can configure the generator to omit the bias-adjustment operations, and gain a little efficiency, by specifying the optional named parameter “comp=0” when the Latin Square generator object is instantiated.

Metadata

Release files for rlsmoc 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rlsmoc 1.0
File Size Uploaded Provenance Actions
rlsmoc-1.0.tar.gz 420.7 kB Without provenance Details for rlsmoc-1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rlsmoc 1.0
File Interpreter ABI Platform Actions
rlsmoc-1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for rlsmoc-1.0-py2.py3-none-any.whl

Total release size: 431.0 kB

Release files / rlsmoc-1.0.tar.gz

Download URL rlsmoc-1.0.tar.gz
Size 420.7 kB
Tags Source
SHA-256 checksum
How to use checksums
757d6df3bf1630cda05c6abc5ace345be1a7187cda4b0cd5631786462063c66a
BLAKE2b-256 checksum
How to use checksums
612de342db9f24bfc6470ad3cbfe0bd3c22259ace1ecd34f200c7437d1828778
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / rlsmoc-1.0-py2.py3-none-any.whl

Download URL rlsmoc-1.0-py2.py3-none-any.whl
Size 10.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
07bf5a190bb143979ed66e1dcbe5fc965cf9e42633ac76947c8c378da1d2c6e9
BLAKE2b-256 checksum
How to use checksums
fd8bcf583c27180a0214a167cfa3c4f623c96627c6a8db54be01668935858103
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for rlsmoc 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page