最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Best practice for function-level reproducibility of random number generation with NumPy - Stack Overflow

programmeradmin1浏览0评论

I am aware of suggested best practices for random number generation in NumPy, which basically boil down to:

  • Use the new API, via np.random.default_rng and related functions; and
  • Set a seed at the beginning of your program to ensure reproducibility throughout.

My question is, what if we want to ensure equal results from random number generation within individual functions? With the goal being that you give a generator/seed argument at every function call and every time you pass it the same generator/seed, you get equivalent results.

My initial thought was to use an rng variable in the function body as you would normally (if you were setting and fetting at the start of your script) and then either set a function parameter like rng=np.random.default_rng() expecting a Generator object or seed (to be passed to an np.random.default_rng(seed) call at the start of the function body).

However, this seems like it goes against best practices as you would be creating multiple, local, temporary Generator objects. What is the "proper" solution to this problem?

发布评论

评论列表(0)

  1. 暂无评论