Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我运行我的函数时,我的值x和y值在每次运行时都保持不变。我不确定我是否错误地使用了 default_random_engine。如何在每次程序执行时使用随机化而不是相同的输出来 获取我的x和y值?dist(engine)
x
y
dist(engine)
“种子”是为伪数生成器提供“随机性”的东西。如果您使用相同的种子对其进行初始化,您将获得相同的“随机”模式。
在这里,您可能一直在传递相同的种子。
也许你应该像这样定义你的函数:
long double fn( long reps, default_random_engine & engine )
运行程序时声明随机数生成器的单个实例,并为其播种一次,该值在程序启动时总是不同的值(例如系统时间)。然后,将生成器传递给任何需要生成随机数的函数。