How to create a table automatically for a homework in statistics?

I suspected that your question How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables? was of the XY type.

The usual problem in these cases is that you cannot build a table inside a \foreach statement, because table cells form groups. The strategy is to build the table body beforehand.

\documentclass{article}

\usepackage{booktabs,etoolbox}

\usepackage{pgf}
\usepackage{tikz}
\usepgflibrary{fpu}

\pgfmathsetseed{\number\pdfrandomseed}

\begin{document}

\def\Sum{0}
\def\Rand{0}
\def\Avg{0}
\def\Var{0}
\def\Sq{0}
\def\TableBody{}
\foreach \i in {1,...,15} {
  %\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
  \pgfmathparse{random(10)}
  \xdef\Rand{\pgfmathresult}
  \pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
  \pgfmathparse{\Sum+\Rand}
  \xdef\Sum{\pgfmathresult}
  \pgfmathparse{\Sq+\Rand*\Rand}
  \xdef\Sq{\pgfmathresult}
  \pgfmathparse{\Sum/\i}
  \xdef\Avg{\pgfmathresult}
  \pgfmathparse{\Sq/\i-\Avg*\Avg}
  \xdef\Var{\pgfmathresult}
  \xappto\TableBody{\i & \Rand & \Sum & \Sq & \Avg & \Var \noexpand\\}
}

\begin{tabular}{ *{6}{c} }
\toprule
Trial & Rand & Sum & Sum-of-squares & Average & Variance \\
\midrule
\TableBody
\bottomrule
\end{tabular}

\end{document}

enter image description here

Here's what I get with longtable and 500 draws.

enter image description here


I am no expert in pgfplotstable but here is an idea to build upon.

To get the entries to align prettier, see Q131081.

(I find pgfplotstable overly complicated and it surely doesn't help that the manual repeats parts of the PGFmanual …)

Code

\documentclass{article}
\usepackage{booktabs,pgfplotstable}
\pgfmathsetseed{\number\pdfrandomseed}
\pgfplotstableset{
  duck ini/.style={columns={Trial,Rand}},
  duck table/.style={
    columns={Trial,Rand,Sum,Sq,Avg,Var},
    set column name/.list={Sq:Sum-of-squared, Avg:Average, Var:Variance},
    every head row/.append style={before row=\toprule, after row=\midrule},
    every last row/.append style={after row=\bottomrule},
    set column/.list={Avg:prec=1, Var:prec=2}},
  set column name/.style args={#1:#2}{columns/#1/.append style={column name={#2}}},
  set column/.style args={#1:#2}{columns/#1/.append style={#2}},
  set expr/.style args={#1=#2}{create on use/#1/.style={create col/expr={#2}}}}
\pgfset{number format/prec/.style={fixed, fixed zerofill, precision={#1}}}
\pgfplotstableset{set expr/.list={Trial=\pgfplotstablerow+1,Rand=int(rnd*11)}}
\newcommand*\addmyusualcolumns[1]{%
  \pgfplotstablecreatecol[expr={\pgfmathaccuma+\thisrow{Rand}}]                           {Sum} #1
  \pgfplotstablecreatecol[expr={\pgfmathaccuma+\thisrow{Rand}*\thisrow{Rand}}]            {Sq}  #1
  \pgfplotstablecreatecol[expr={\thisrow{Sum}/\thisrow{Trial}}]                           {Avg} #1
  \pgfplotstablecreatecol[expr={\thisrow{Sq}/\thisrow{Trial}-\thisrow{Avg}*\thisrow{Avg}}]{Var} #1}
\begin{document}
\pgfplotstablenew[duck ini]{15}\t
\addmyusualcolumns\t
\pgfplotstabletypeset[duck table]\t

\pgfplotstablenew[duck ini]{30}\T
\addmyusualcolumns\T
\pgfplotstabletypeset[duck table]\T
\end{document}