MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / RNG

Class RNG

imperative/python/megengine/random/rng.py:287–709  ·  view source on GitHub ↗

r""":class:`RNG` exposes a number of methods for generating random numbers. Args: seed: random seed used to initialize the pseudo-random number generator. Default: None device: the device of generated tensor. Default: None Examples: >>> import megengine.random as r

Source from the content-addressed store, hash-verified

285
286
287class RNG:
288
289 r""":class:`RNG` exposes a number of methods for generating random numbers.
290
291 Args:
292 seed: random seed used to initialize the pseudo-random number generator. Default: None
293 device: the device of generated tensor. Default: None
294
295
296 Examples:
297 >>> import megengine.random as rand
298 >>> rng = rand.RNG(seed=100)
299 >>> x = rng.uniform(size=(2, 2))
300 >>> x.numpy() # doctest: +SKIP
301 array([[0.84811664, 0.6147553 ],
302 [0.59429836, 0.64727545]], dtype=float32)
303 """
304
305 def __init__(self, seed: int = None, device: str = None):
306 self._device = device if device else get_default_device()
307 if seed is not None:
308 self._seed = seed
309 self._handle = _new_rng_handle(self._device, self._seed)
310 else:
311 self._seed = _get_global_rng_seed
312 self._handle = 0
313 self._device = None
314
315 def uniform(
316 self, low: float = 0, high: float = 1, size: Optional[Iterable[int]] = None
317 ):
318 r"""Random variable with uniform distribution :math:`U(low, high)`.
319
320 Args:
321 low(float): lower range. Default: 0.
322 high(float): upper range. Default: 1.
323 size(Optional[Iterable[int]]): the size of output tensor. Default: None.
324
325 Returns:
326 Return type: tensor. The random variable with uniform distribution.
327
328 Examples:
329 >>> import megengine.random as rand
330 >>> x = rand.uniform(size=(2, 2))
331 >>> x.numpy() # doctest: +SKIP
332 array([[0.28603864, 0.3156649 ],
333 [0.42066026, 0.9805052 ]], dtype=float32)
334 """
335 _seed = self._seed() if callable(self._seed) else self._seed
336 return _uniform(
337 low=low,
338 high=high,
339 size=size,
340 seed=_seed,
341 device=self._device,
342 handle=self._handle,
343 )
344

Callers 15

test_UniformRNGFunction · 0.90
test_NormalRNGFunction · 0.90
test_GammaRNGFunction · 0.90
test_BetaRNGFunction · 0.90
test_PoissonRNGFunction · 0.90
test_MultinomialRNGFunction · 0.90
test_PermutationRNGFunction · 0.90
test_ShuffleRNGFunction · 0.90
test_ExponentialRNGFunction · 0.90
_default_rngFunction · 0.70
__init__Method · 0.50
seedMethod · 0.50

Calls

no outgoing calls

Tested by 9

test_UniformRNGFunction · 0.72
test_NormalRNGFunction · 0.72
test_GammaRNGFunction · 0.72
test_BetaRNGFunction · 0.72
test_PoissonRNGFunction · 0.72
test_MultinomialRNGFunction · 0.72
test_PermutationRNGFunction · 0.72
test_ShuffleRNGFunction · 0.72
test_ExponentialRNGFunction · 0.72