Generating random symmetric matrix

ClearAll[randomSymMat];
randomSymMat = Module[{mat = RandomVariate[#, {#2, #2}], upper, diag},
    upper = UpperTriangularize[mat, 1];
    diag = DiagonalMatrix[Diagonal@mat];
    diag + upper + Transpose[upper]] &;

dist = NormalDistribution[3, 5];
First[AbsoluteTiming[res = randomSymMat[dist, 1000]]]
(* 0.065046 *)
res == Transpose[res]
(* True *)

A slightly simpler code: The presence of the 1 in UpperTriangularize[] grabs the strictly upper triangular components. This way you don't need to define the diag portion for the sum.

    (* Generate symmetric n × m matrix with 
      component values as random reals in (0,K) *)
    RM = RandomReal[K, {n, m}]; 
    SM = UpperTriangularize[RM] + Transpose[UpperTriangularize[RM, 1]]

Of course you can change the RandomReal[] to whatever method you want to generate your entries.


If you have Mathematica 10.3, this is doable by the following code:

randSym[dist_, n_] := Module[{diag, up},
    diag = RandomVariate[dist, n];
    up = RandomVariate[dist, Binomial[n, 2]];
    Statistics`Library`VectorToSymmetricMatrix[up, diag, n]
]

This method does not sample unnecessary entries hence it is faster. It pastes the sampled values properly into a symmetric matrix.