如何访问numpy默认全局随机数生成器 [英] How to access numpy default global random number generator

查看:103
本文介绍了如何访问numpy默认全局随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个将随机数生成器(即numpy.random.RandomState对象)作为参数的类.在未指定此参数的情况下,我想将其分配给运行numpy.random.<random-method>时numpy使用的随机生成器.如何访问此全局生成器?目前,我只是通过将模块对象分配为随机生成器来完成此操作(因为它们共享方法/鸭子类型).但是,这会在酸洗(无法酸洗模块对象)和深度复制时引起问题.我想在numpy.random后面使用RandomState对象

I need to create a class which takes in a random number generator (i.e. a numpy.random.RandomState object) as a parameter. In the case this argument is not specified, I would like to assign it to the random generator that numpy uses when we run numpy.random.<random-method>. How do I access this global generator? Currently I am doing this by just assigning the module object as the random generator (since they share methods / duck typing). However this causes issues when pickling (unable to pickle module object) and deep-copying. I would like to use the RandomState object behind numpy.random

PS:我正在使用python-3.4

PS: I'm using python-3.4

推荐答案

以及kazemakase提出的建议,我们可以利用以下事实:像numpy.random.random这样的模块级函数实际上是隐藏numpy.random.RandomState的方法.直接从其中一种方法中提取__self__:

As well as what kazemakase suggests, we can take advantage of the fact that module-level functions like numpy.random.random are really methods of a hidden numpy.random.RandomState by pulling the __self__ directly from one of those methods:

numpy_default_rng = numpy.random.random.__self__

这篇关于如何访问numpy默认全局随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆