当前位置: 首页>>代码示例>>Python>>正文


Python _random.Random方法代码示例

本文整理汇总了Python中_random.Random方法的典型用法代码示例。如果您正苦于以下问题:Python _random.Random方法的具体用法?Python _random.Random怎么用?Python _random.Random使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在_random的用法示例。


在下文中一共展示了_random.Random方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _test

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def _test(N=2000):
    _test_generator(N, random, ())
    _test_generator(N, normalvariate, (0.0, 1.0))
    _test_generator(N, lognormvariate, (0.0, 1.0))
    _test_generator(N, vonmisesvariate, (0.0, 1.0))
    _test_generator(N, gammavariate, (0.01, 1.0))
    _test_generator(N, gammavariate, (0.1, 1.0))
    _test_generator(N, gammavariate, (0.1, 2.0))
    _test_generator(N, gammavariate, (0.5, 1.0))
    _test_generator(N, gammavariate, (0.9, 1.0))
    _test_generator(N, gammavariate, (1.0, 1.0))
    _test_generator(N, gammavariate, (2.0, 1.0))
    _test_generator(N, gammavariate, (20.0, 1.0))
    _test_generator(N, gammavariate, (200.0, 1.0))
    _test_generator(N, gauss, (0.0, 1.0))
    _test_generator(N, betavariate, (3.0, 3.0))
    _test_generator(N, triangular, (0.0, 1.0, 1.0/3.0))

# Create one instance, seeded from current time, and export its methods
# as module-level functions.  The functions share state across all uses
#(both in the user's code and in the Python libraries), but that's fine
# for most programs and is easier for the casual user than making them
# instantiate their own Random() instance. 
开发者ID:war-and-code,项目名称:jawfish,代码行数:25,代码来源:random.py

示例2: seed

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def seed(self, a=None):
        """Initialize internal state from hashable object.

        None or no argument seeds from current time or from an operating
        system specific randomness source if available.

        If a is not None or an int or long, hash(a) is used instead.
        """

        if a is None:
            try:
                a = long(_hexlify(_urandom(16)), 16)
            except NotImplementedError:
                import time
                a = long(time.time() * 256) # use fractional seconds

        super(Random, self).seed(a)
        self.gauss_next = None 
开发者ID:glmcdona,项目名称:meddle,代码行数:20,代码来源:random.py

示例3: setstate

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def setstate(self, state):
        """Restore internal state from object returned by getstate()."""
        version = state[0]
        if version == 3:
            version, internalstate, self.gauss_next = state
            super(Random, self).setstate(internalstate)
        elif version == 2:
            version, internalstate, self.gauss_next = state
            # In version 2, the state was saved as signed ints, which causes
            #   inconsistencies between 32/64-bit systems. The state is
            #   really unsigned 32-bit ints, so we convert negative ints from
            #   version 2 to positive longs for version 3.
            try:
                internalstate = tuple( long(x) % (2**32) for x in internalstate )
            except ValueError, e:
                raise TypeError, e
            super(Random, self).setstate(internalstate) 
开发者ID:glmcdona,项目名称:meddle,代码行数:19,代码来源:random.py

示例4: jumpahead

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def jumpahead(self, n):
        """Change the internal state to one that is likely far away
        from the current state.  This method will not be in Py3.x,
        so it is better to simply reseed.
        """
        # The super.jumpahead() method uses shuffling to change state,
        # so it needs a large and "interesting" n to work with.  Here,
        # we use hashing to create a large n for the shuffle.
        s = repr(n) + repr(self.getstate())
        n = int(_hashlib.new('sha512', s).hexdigest(), 16)
        super(Random, self).jumpahead(n)

## ---- Methods below this point do not need to be overridden when
## ---- subclassing for the purpose of using a different core generator.

## -------------------- pickle support  ------------------- 
开发者ID:glmcdona,项目名称:meddle,代码行数:18,代码来源:random.py

示例5: whseed

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def whseed(self, a=None):
        """Seed from hashable object's hash code.

        None or no argument seeds from current time.  It is not guaranteed
        that objects with distinct hash codes lead to distinct internal
        states.

        This is obsolete, provided for compatibility with the seed routine
        used prior to Python 2.1.  Use the .seed() method instead.
        """

        if a is None:
            self.__whseed()
            return
        a = hash(a)
        a, x = divmod(a, 256)
        a, y = divmod(a, 256)
        a, z = divmod(a, 256)
        x = (x + a) % 256 or 1
        y = (y + a) % 256 or 1
        z = (z + a) % 256 or 1
        self.__whseed(x, y, z)

## --------------- Operating System Random Source  ------------------ 
开发者ID:glmcdona,项目名称:meddle,代码行数:26,代码来源:random.py

示例6: seed

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def seed(self, a=None):
        """Initialize internal state of the random number generator.

        None or no argument seeds from current time or from an operating
        system specific randomness source if available.

        If a is not None or is an int or long, hash(a) is used instead.
        Hash values for some types are nondeterministic when the
        PYTHONHASHSEED environment variable is enabled.
        """

        if a is None:
            try:
                # Seed with enough bytes to span the 19937 bit
                # state space for the Mersenne Twister
                a = long(_hexlify(_urandom(2500)), 16)
            except NotImplementedError:
                import time
                a = long(time.time() * 256) # use fractional seconds

        super(Random, self).seed(a)
        self.gauss_next = None 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:24,代码来源:random.py

示例7: test_setstate

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def test_setstate(self):
        # state is object which
        random = _random.Random()
        state1 = random.getstate()
        random.setstate(state1)
        state2 = random.getstate()
        self.assertEqual(state1,state2)

        random.jumpahead(1)
        self.assertTrue(state1 != random.getstate())

        random.setstate(state1)
        self.assertEqual(state1, random.getstate())

        #state is a int object
        a = 1
        self.assertRaises(Exception,random.setstate,a)

        #state is a string object
        b = "stete"
        self.assertRaises(Exception,random.setstate,b)

        #state is a random object
        c = _random.Random()
        self.assertRaises(Exception,random.setstate,c) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test__random.py

示例8: test_random

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def test_random(self):    
        import _random
        random = _random.Random()
        from System.Threading import Thread, ParameterizedThreadStart    
        global zeroCount
        zeroCount = 0
        
        def foo((ntimes,nbits)):
            for i in xrange(ntimes):
                x = random.getrandbits(nbits)
                if x == 0:
                    zeroCount += 1
        
        def run_many(nthreads,ntimes,nbits):
            lst_threads = []
            for i in xrange(nthreads):
                t = Thread(ParameterizedThreadStart(foo))
                t.Start((ntimes,nbits))
                lst_threads.append(t)
            for t in lst_threads:
                t.Join()
        
        run_many(10,10**6,63)
        self.assertTrue(zeroCount < 3) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:26,代码来源:test_threadsafety.py

示例9: seed

# 需要导入模块: import _random [as 别名]
# 或者: from _random import Random [as 别名]
def seed(self, a=None):
        """Initialize internal state from hashable object.

        None or no argument seeds from current time or from an operating
        system specific randomness source if available.

        If a is not None or an int or long, hash(a) is used instead.
        """

        if a is None:
            try:
                # Seed with enough bytes to span the 19937 bit
                # state space for the Mersenne Twister
                a = long(_hexlify(_urandom(2500)), 16)
            except NotImplementedError:
                import time
                a = long(time.time() * 256) # use fractional seconds

        super(Random, self).seed(a)
        self.gauss_next = None 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:22,代码来源:random.py


注:本文中的_random.Random方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。