<< DiscreteMath`Combinatorica`;
\(\(randomprofile[n_, m_] := \((pairlist = {}; 
          relprobabilities = {}; 
pn = Partitions[n]; 
          pm = Partitions[m]; 
For[i = 1, 
            i ≤ Length[pn], \(i++\), 
            For[j = 1, j ≤ Length[pm], \(j++\), 
              lambda = pn[\([i]\)]; 
              mu = pm[\([j]\)]; 
If[
                IntegerQ[partGCD[lambda]/partLCM[mu]], 
                pairlist = 
                  Append[pairlist, {lambda, 
                      mu}]; 
relprobabilities = 
                  Append[relprobabilities, \((Factorial[m]^
                          Sum[Count[lambda, kk], {kk, 1, n}])\)/\((zla[
                            lambda, n]*
                          zla[mu, 
                            m])\)]]]]; 

probabilities = 
            relprobabilities/
              Sum[relprobabilities[\([r]\)], {r, 1, 
                  Length[relprobabilities]}]; 

profile = 
            Table[0, {j, 1, m}, {i, 1, n}]; 
prefixsum = 
            probabilities[\([1]\)]; x = Random[]; 
          rank = 1; 
While[x > prefixsum, \(rank++\); 
            prefixsum = 
              prefixsum + 
                probabilities[\([rank]\)]]; 
lambda = 
            pairlist[\([rank, 1]\)]; 
          mu = pairlist[\([rank, 
                2]\)]; 

 (*\ 
            At\ this\ point\ \((lambda, mu)\)\ is\ a\ random\ pair . 
                Now\ generate\ the\ cycle\ structure\ for\ sigma\ \(\((canonical\ permutation\ of\ type\ lambda)\)\(.\)\)*) 
            sigmacycles = {}; 
For[s = 1, 
            s ≤ n, \(s++\), 
            sigmacycles = 
              Append[sigmacycles, 
                Count[lambda, 
                  s]]]; 
 (*Now\ generate\ sigma\ in\ cycle\ form*) sigmacycleform = {}; 
          ind = 1; 
For[s = 1, s ≤ n, \(s++\), 
            cs = sigmacycles[\([s]\)]; 
For[t = 1, 
              t ≤ cs, \(t++\), 
              temp = {}; 
For[z = 1, 
                z ≤ s, \(z++\), 
                temp = Append[temp, 
                    ind]; \(ind++\)]; 
sigmacycleform = 
                Append[sigmacycleform, 
                  temp]]]; 
 (*Now\ sigmacycleform\ is\ sigma\ in\ cycle\ notation . Convert\ to\ 1 - line\ notation*) \ 
          sigma = FromCycles[
              sigmacycleform]; 
 (*Do\ the\ same\ and\ generate\ canonical\ tau\ corresponding\ to\ mu*) 
taucycles = {}; 
For[s = 1, 
            s ≤ m, \(s++\), 
            taucycles = 
              Append[taucycles, 
                Count[mu, 
                  s]]]; 
 (*Now\ generate\ tau\ in\ cycle\ form*) taucycleform = {}; 
          ind = 1; 
For[s = 1, s ≤ m, \(s++\), 
            cs = taucycles[\([s]\)]; 
For[t = 1, 
              t ≤ cs, \(t++\), 
              temp = {}; 
For[z = 1, 
                z ≤ s, \(z++\), 
                temp = Append[temp, 
                    ind]; \(ind++\)]; 
taucycleform = 
                Append[taucycleform, 
                  temp]]]; 
 (*Now\ taucycleform\ is\ tau\ in\ cycle\ notation . \ Convert\ to\ 1 - 
              line\ notation*) \ 
tau = 
            FromCycles[
              taucycleform]; 
 (*Now\ generate\ table\ column\ by\ column . First\ 1 - cycles, 
            then\ 2 - 
              cycles, \(\(etc\)\(.\)\)*) 
columnindex = 
            0; 
For[s = 1, 
            s ≤ 
              n, \(s++\),  (*Now\ looking\ at\ s - 
                cycles*) 
cs = 
              sigmacycles[\([s]\)]; 
 (*There\ are\ cs\ s - 
                cycles\ to\ consider*) 
For[t = 1, 
              t ≤ 
                cs, \(t++\),  (*Process\ t - th\ s - 
                  cycle . These\ are\ s\ columns\ to\ process\ for\ each\ s - 
                  cycle*)  (*First\ pick\ the\ first\ column\ for\ this\ cycle\ a\ random\ permutation\ randomtau\ of\ m*) 
randomtau = 
                RandomPermutation[
                  m]; 
 (*Place\ it*) 
columnindex = columnindex + 1; 
For[
                z = 1, z ≤ m, \(z++\), 
                profile[\([z, columnindex]\)] = 
                  randomtau[\([z]\)]]; 
 (*Now\ place\ iterates\ under\ original\ tau*) For[c = 2, c ≤ s, \(c++\), 
                newtau = randomtau; 
                For[z = 1, z ≤ m, \(z++\), 
                  newtau[\([z]\)] = 
                    profile[\([z, 
                        columnindex]\)]];  (*Read\ previous\ column*) 
columnindex = columnindex + 1; 
For[
                  z = 1, z ≤ m, \(z++\), 
                  profile[\([z, columnindex]\)] = \(comp[tau, 
                        newtau]\)[\([z]\)]];
]]]; 
          Print["Random profile= ", 
            profile // MatrixForm])\);\)\)
partLCM[p_] := 
        Module[{k}, res1 = 1; 
          For[k = 1, k ≤ Length[p], k++, 
            res1 = LCM[res1, p[[k]]]]; res1];
partGCD[p_] := 
        Module[{k}, res2 = p[[1]]; 
          For[k = 2, k ≤ Length[p], k++, 
            res2 = GCD[res2, p[[k]]]]; res2];
zla[p_, n_] := 
        Module[{k}, 
          Product[(k^Count[p, k])*Factorial[Count[p, k]], {k, 1, 
              n}]];
comp[sigma_, tau_] := 
      Module[{k}, compres = {}; 
        For[k = 1, k ≤ Length[tau], k++, 
          compres = Append[compres, sigma[[tau[[k]]]]]]; 
        compres]

randomvote[30, 9]

For[g = 1, g ≤ 10, g++, randomprofile[3, 2]]

profile