Generate multiple independent random streams in py

2019-05-06 04:47发布

I want to generate multiple streams of random numbers in python. I am writing a program for simulating queues system and want one stream for the inter-arrival time and another stream for the service time and so on.

numpy.random() generates random numbers from a global stream.

In matlab there is something called RandStream which enables me to create multiple streams.

Is there any way to create something like RandStream in Python

2条回答
小情绪 Triste *
2楼-- · 2019-05-06 05:37

Both Numpy and the internal random generators have instantiatable classes.

For just random:

import random
random_generator = random.Random()
random_generator.random()
#>>> 0.9493959884174072

And for Numpy:

import numpy
random_generator = numpy.random.RandomState()
random_generator.uniform(0, 1, 10)
#>>> array([ 0.98992857,  0.83503764,  0.00337241,  0.76597264,  0.61333436,
#>>>         0.0916262 ,  0.52129459,  0.44857548,  0.86692693,  0.21150068])
查看更多
何必那么认真
3楼-- · 2019-05-06 05:48

Veedrac's answer did not address how one might generate independent streams.

The best way I could find to generate independent streams is to use a replacement for numpy's RandomState. This is provided by the RandomGen package.

It supports independent random streams, but these use one of three random number generators: PCG64, ThreeFry or Philox. If you want to use the more conventional MT19937, you can rely on jumping instead.

查看更多
登录 后发表回答