Random Latin squares: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: forgot about two 5s)
(Realize in F#)
Line 22: Line 22:
<br>
<br>


=={{header|F_Sharp|F#}}==
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>
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")
printfn ""
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")
</lang>
{{out}}
<pre>
[|1; 3; 2; 4; 5|]
[|4; 5; 3; 2; 1|]
[|2; 4; 1; 5; 3|]
[|3; 2; 5; 1; 4|]
[|5; 1; 4; 3; 2|]

[|2; 3; 4; 1; 5|]
[|3; 2; 5; 4; 1|]
[|5; 4; 1; 2; 3|]
[|1; 5; 2; 3; 4|]
[|4; 1; 3; 5; 2|]
</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.