![如何获取伪代码中对应行的正确数字?](https://linux22.com/image/454510/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BC%AA%E4%BB%A3%E7%A0%81%E4%B8%AD%E5%AF%B9%E5%BA%94%E8%A1%8C%E7%9A%84%E6%AD%A3%E7%A1%AE%E6%95%B0%E5%AD%97%EF%BC%9F.png)
\begin{algorithm}
\caption{Random Vector Generation}
\begin{algorithmic}
\State Generate $\mathit{n}$ random variables $\mathit{U_{1},...,U_{n}}$ as iid variables from $\mathit{U(0,1)}$ \newline
\State Set $X_{1} = 1- X_{n},...,X_{n} = 1- 2U_{n}$ and $\mathit{R = \sum_{i=1}^n X_{i}^2}$ \newline
\State if $\mathit{R \leq 1}$, accept $\mathit{\textbf{X} = (X_{1},....X_{n})^T}$ as the desired vector; otherwise go to step 1.
\end{algorithmic}
\end{algorithm}
答案1
我不知道你是怎么得到那张照片的,因为电话
\begin{algorithmic}
相当于
\begin{algorithmic}[0]
意思是“没有行号”。你想要
\begin{algorithmic}[1]
意思是“给每行编号”。
这是有效的代码,使用了更好的技巧来\newline
分隔线条。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Random Vector Generation}
\begin{algorithmic}[1]
\setlength{\itemsep}{1ex}
\State Generate $n$ random variables $U_{1},\dots,U_{n}$ as iid variables from
$U(0,1)$
\State Set $X_{1} = 1- 2U_{1},\dots,X_{n} = 1- 2U_{n}$ and $R = \sum_{i=1}^n X_{i}^2$
\State if $R \leq 1$, accept $\mathbf{X} = (X_{1},\dots,X_{n})^T$ as the desired vector;
otherwise go to step~1.
\end{algorithmic}
\end{algorithm}
\end{document}
请注意,我删除了所有\mathit
声明并将其转换...
为\dots
。我还修复了X_{1}=1-2U_{1}
。