How can I create a random symmetric matrix in this way?
First, you just have to create exactly one H[N]
and use this 2 times. This can be achieved by
H[n_] := RandomReal[{0, 10}, {n, n}]
symmetryH[n_] := With[{hn = H[n]},
(hn + Transpose[hn])/2
]
Second, please do not use N
as variable or pattern. It might shoot you in the foot because it is a function in Mathematica.
Finally, you should realize, that your approach contains a very serious flaw. What makes you believe, that you can calculate the mean of your matrix and its transpose while still maintain the same distribution for your randomness? Let us make a very quick example, where we create 1000 of your random matrices:
data = Table[symmetryH[4], {1000}];
Now, let us look on all upper left elements and plot their histogram
Histogram[data[[All, 1, 1]]]
Exactly what you wanted. Evenly distributed. Let's take a look on the upper right corner
Histogram[data[[All, 1, -1]]]
Uhh, not so nice. This happens because all upper right elements are really the mean of two different numbers, while the upper left elements are the mean of the same number.
This one prevents the distribution problems halirutan is talking about:
h[n_] :=
Module[{m},
m = RandomReal[{0, 10}, {n, n}];
m SparseArray[{i_, j_} /; i >= j -> 1, {n, n}] +
Transpose[m SparseArray[{i_, j_} /; i > j -> 1, {n, n}]] // Normal
]
h[5] // TableForm
Distribution is uniform now:
Histogram@Flatten@h[1000]