Random Latin squares: Difference between revisions

Content added Content deleted
(Realize in F#)
Line 25: Line 25:
This solution uses functions from [[Factorial_base_numbers_indexing_permutations_of_a_collection#F.23]] and [[Latin_Squares_in_reduced_form#F.23]]. This solution generates completely random uniformly distributed Latin Squares from all possible Latin Squares of order 5. Unlike other solutions on this page, even those claiming to do so! see [[Talk:Random_Latin_Squares#.22restarting_row.22_method]]. It takes 5 thousandths of a second can that really be called hard?
This solution uses functions from [[Factorial_base_numbers_indexing_permutations_of_a_collection#F.23]] and [[Latin_Squares_in_reduced_form#F.23]]. This solution generates completely random uniformly distributed Latin Squares from all possible Latin Squares of order 5. Unlike other solutions on this page, even those claiming to do so! see [[Talk:Random_Latin_Squares#.22restarting_row.22_method]]. It takes 5 thousandths of a second can that really be called hard?
<lang fsharp>
<lang fsharp>
// Generate 2 Random Latin Squares of order 5. Nigel Galloway: July 136th., 2019
let N=let N=System.Random() in (fun n->N.Next(n))
let N=let N=System.Random() in (fun n->N.Next(n))
let β=lN2p [|0;N 3;N 2;N 1|] [|0..4|] in Seq.item (N 55) (normLS 5) |> List.map(lN2p [|N 4;N 3;N 2;N 1|]) |> List.permute(fun n->β.[n]) |> List.iter(printfn "%A")
let β=lN2p [|0;N 3;N 2;N 1|] [|0..4|] in Seq.item (N 55) (normLS 5) |> List.map(lN2p [|N 4;N 3;N 2;N 1|]) |> List.permute(fun n->β.[n]) |> List.iter(printfn "%A")
Line 44: Line 45:
[|4; 1; 3; 5; 2|]
[|4; 1; 3; 5; 2|]
</pre>
</pre>

// Generate 2 Random Latin Squares of order 5. Nigel Galloway: July 136th., 2019
=={{header|Factor}}==
=={{header|Factor}}==
The initial approach is simple: generate a random permutation, one row at a time. If a row conflicts with any of the rows above it, generate a new random permutation for that row. The upside is that this is easy to understand and generates uniformly-random Latin squares. The downside is that it is an exponential time algorithm.
The initial approach is simple: generate a random permutation, one row at a time. If a row conflicts with any of the rows above it, generate a new random permutation for that row. The upside is that this is easy to understand and generates uniformly-random Latin squares. The downside is that it is an exponential time algorithm.