Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为PythonTeX文档设置随机种子_Python_Latex_Random Seed - Fatal编程技术网

为PythonTeX文档设置随机种子

为PythonTeX文档设置随机种子,python,latex,random-seed,Python,Latex,Random Seed,我是一名物理教师,我想用PythonTeX来写带有随机值的乳胶的问题 我想在文档上运行pythontex3.py并获得一组新的随机值。据我所知,python随机模块将从系统时钟中选择一个种子,这样在启动python时就不会总是得到相同的随机数集 然而,pythontex3.py似乎总是以相同的种子开始,因此每次都生成相同的伪随机数集 这是一个MWE \documentclass{exam} \usepackage{pythontex} \begin{document}

我是一名物理教师,我想用PythonTeX来写带有随机值的乳胶的问题

我想在文档上运行pythontex3.py并获得一组新的随机值。据我所知,python随机模块将从系统时钟中选择一个种子,这样在启动python时就不会总是得到相同的随机数集

然而,pythontex3.py似乎总是以相同的种子开始,因此每次都生成相同的伪随机数集

这是一个MWE

    \documentclass{exam}
    \usepackage{pythontex}

    \begin{document}
    \begin{pycode}
    import random
    # Create large number with 3 significant digits
    Number=random.randint(100,1000)*1000
    \end{pycode}
    
    \begin{questions}
    \question
    The number \pyc{print(Number)} in scientific notation is
    \begin{choices}
        \CorrectChoice \pyc{print('%.2E' % Number)}
        \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-10,-5)))}
        \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-5,0)))}
        \choice \pyc{print('%.2E' % (Number*1*10**random.randint(5,10)))}
    \end{choices}
    \end{questions}
    \end{document}
这将在我的机器上生成以下内容。

为什么不在代码中明确提供随机种子

random.seed() # use system time as a randomness source
或者,您可以使用

random.seed(2345)

为什么不在代码中显式提供随机种子

random.seed() # use system time as a randomness source
或者,您可以使用

random.seed(2345)

解决方案是用系统的时间设置随机种子,然后告诉python始终运行python代码。默认情况下,只有在python代码自上次编译以来发生更改时,pythontex才会更新

\documentclass{exam}
\usepackage{pythontex}

\begin{document}
\begin{pycode}
import random
# Set seed by clock's microsecond so that each 
# compilation will give new random values
from datetime import datetime
now=datetime.now()
# Manually set seed if reproducible sequence is desired
random.seed(now.microsecond)
# Create large number with 3 significant digits
Number=random.randint(100,1000)*1000
\end{pycode}
\begin{questions}
\question
The number \pyc{print(Number)} in scientific notation is
\begin{choices}
    \CorrectChoice \pyc{print('%.2E' % Number)}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-10,-5)))}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-5,0)))}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(5,10)))}
\end{choices}
\end{questions}
\end{document}
然后用

pdflatex MWE.tex;pythontex3-runall=true MWE.tex;pdflatex MWE.tex


解决方案是用系统的时间设置随机种子,然后告诉python始终运行python代码。默认情况下,只有在python代码自上次编译以来发生更改时,pythontex才会更新

\documentclass{exam}
\usepackage{pythontex}

\begin{document}
\begin{pycode}
import random
# Set seed by clock's microsecond so that each 
# compilation will give new random values
from datetime import datetime
now=datetime.now()
# Manually set seed if reproducible sequence is desired
random.seed(now.microsecond)
# Create large number with 3 significant digits
Number=random.randint(100,1000)*1000
\end{pycode}
\begin{questions}
\question
The number \pyc{print(Number)} in scientific notation is
\begin{choices}
    \CorrectChoice \pyc{print('%.2E' % Number)}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-10,-5)))}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(-5,0)))}
    \choice \pyc{print('%.2E' % (Number*1*10**random.randint(5,10)))}
\end{choices}
\end{questions}
\end{document}
然后用

pdflatex MWE.tex;pythontex3-runall=true MWE.tex;pdflatex MWE.tex


手动设置种子是一个好主意,我现在将使用它。我尝试使用x=datetime.now random.seedx.microsecond在每次编译时获取不同的种子,但没有成功。手动设置种子是一个好主意,我现在将使用它。每次编译时,我都尝试使用x=datetime.now random.seedx.microsecond来获得不同的种子,但没有成功。