# How to generate a "value-frequency" table with random numbers?

I want to create a "value-frequency" table from the following expression.

Counts[Sort[RandomInteger[{1, 5}, 500]]] // TableForm


The generated table should be in LaTeX form either

• two rows and n columns. first row: Value, second row: Frequency
• n rows and 2 columns. first column: Value, second column: Frequency

Optionally with headings "Value" and "Frequency" to make it look nice.

How to achieve this?

SeedRandom
counts = Counts[Sort[RandomInteger[{1, 5}, 500]]];
TeXForm @ Grid[Join[{{"Value", "Frequency"}}, KeyValueMap[List][counts]], Dividers -> All]


$$\begin{array}{|c|c|} \hline \text{Value} & \text{Frequency} \\ \hline 1 & 104 \\ \hline 2 & 92 \\ \hline 3 & 97 \\ \hline 4 & 101 \\ \hline 5 & 106 \\ \hline \end{array}$$

TeXForm@Grid[Transpose@Join[{{"Value", "Frequency"}}, KeyValueMap[List][counts]],
Dividers -> All]


$$\begin{array}{|c|c|c|c|c|c|} \hline \text{Value} & 1 & 2 & 3 & 4 & 5 \\ \hline \text{Frequency} & 104 & 92 & 97 & 101 & 106 \\ \hline \end{array}$$

One way to make your table:

TeXForm@TableForm[Sort[Tally@RandomInteger[{1, 5}, 500]],

$$\begin{array}{cc} \text{Value} & \text{Frequency} \\ 1 & 89 \\ 2 & 94 \\ 3 & 117 \\ 4 & 93 \\ 5 & 107 \\ \end{array}$$