Author Topic: Random Latin Squares - Rosetta Code  (Read 844 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Random Latin Squares - Rosetta Code
« on: March 25, 2022, 08:02:07 pm »
See Rosetta Code:  http://rosettacode.org/wiki/Random_Latin_squares

Code: QB64: [Select]
  1. _Title "Random Latin Squares - Rosetta Code" ' b+ 2022-03-25
  2. ' ref http://rosettacode.org/wiki/Random_Latin_squares
  3. Dim test, i, j
  4. ReDim b%(0, 0)
  5. Print "As required by RC:"
  6. For test = 1 To 2
  7.     n = 5
  8.     RandomLatinSquare n, b%()
  9.     displayLatinSquare n, b%()
  10. Print "Showing off:"
  11. For n = 2 To 16
  12.     RandomLatinSquare n, b%()
  13.     displayLatinSquare n, b%()
  14.  
  15. Sub RandomLatinSquare (n As Integer, RtnArray%())
  16.     Dim As Integer a(0 To n - 1), b(0 To n - 1)
  17.     For i = 0 To n - 1
  18.         a(i) = i: b(i) = i
  19.     Next
  20.     For i = n - 1 To 1 Step -1
  21.         Swap a(Int((i + 1) * Rnd)), a(i)
  22.         Swap b(Int((i + 1) * Rnd)), b(i)
  23.     Next
  24.     ReDim RtnArray%(0 To n - 1, 0 To n - 1)
  25.     For i = 0 To n - 1
  26.         For j = 0 To n - 1
  27.             RtnArray%(j, i) = (a(j) + b(i)) Mod n
  28.         Next
  29.     Next
  30.  
  31. Function pad$ (n, nSpaces)
  32.     pad$ = Right$(Space$(nSpaces) + _Trim$(Str$(n)), nSpaces)
  33.  
  34. Sub displayLatinSquare (n As Integer, b%())
  35.     For i = 0 To n - 1
  36.         For j = 0 To n - 1
  37.             If n < 10 Then Print pad$(b%(j, i), 2); Else Print pad$(b%(j, i), 3);
  38.         Next
  39.         Print
  40.     Next
  41.     Print
  42.  
  43.  
  44.  

Some low hanging fruit, no FreeBasic (or any other Basic) version yet!?