【问题标题】:Open-source implementation of Mersenne Twister in Python? [closed]Mersenne Twister 在 Python 中的开源实现? [关闭]
【发布时间】:2011-01-28 23:14:26
【问题描述】:

是否有任何好的 Mersenne Twister 开源实现以及 Python 中其他好的随机数生成器可用?我想用于教授数学和计算机科学专业?我也在寻找相应的理论支持。

编辑: Mersenne Twister 的源代码很容易以各种语言提供,例如 C (random.py) 或伪代码(维基百科),但我在 Python 中找不到。

【问题讨论】:

    标签: python random open-source mersenne-twister


    【解决方案1】:

    Found 以下端口:

    #!/usr/bin/python
    
    ## a C -> python translation of MT19937, original license below ##
    
    ##  A C-program for MT19937: Real number version
    ##    genrand() generates one pseudorandom real number (double)
    ##  which is uniformly distributed on [0,1]-interval, for each
    ##  call. sgenrand(seed) set initial values to the working area
    ##  of 624 words. Before genrand(), sgenrand(seed) must be
    ##  called once. (seed is any 32-bit integer except for 0).
    ##  Integer generator is obtained by modifying two lines.
    ##    Coded by Takuji Nishimura, considering the suggestions by
    ##  Topher Cooper and Marc Rieffel in July-Aug. 1997.
    
    ##  This library is free software; you can redistribute it and/or
    ##  modify it under the terms of the GNU Library General Public
    ##  License as published by the Free Software Foundation; either
    ##  version 2 of the License, or (at your option) any later
    ##  version.
    ##  This library is distributed in the hope that it will be useful,
    ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
    ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    ##  See the GNU Library General Public License for more details.
    ##  You should have received a copy of the GNU Library General
    ##  Public License along with this library; if not, write to the
    ##  Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    ##  02111-1307  USA
    
    ##  Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
    ##  Any feedback is very welcome. For any question, comments,
    ##  see http://www.math.keio.ac.jp/matumoto/emt.html or email
    ##  matumoto@math.keio.ac.jp
    
    
    import sys
    
    # Period parameters
    N = 624
    M = 397
    MATRIX_A = 0x9908b0dfL   # constant vector a
    UPPER_MASK = 0x80000000L # most significant w-r bits
    LOWER_MASK = 0x7fffffffL # least significant r bits
    
    # Tempering parameters
    TEMPERING_MASK_B = 0x9d2c5680L
    TEMPERING_MASK_C = 0xefc60000L
    
    def TEMPERING_SHIFT_U(y):
        return (y >> 11)
    
    def TEMPERING_SHIFT_S(y):
        return (y << 7)
    
    def TEMPERING_SHIFT_T(y):
        return (y << 15)
    
    def TEMPERING_SHIFT_L(y):
        return (y >> 18)
    
    mt = []   # the array for the state vector
    mti = N+1 # mti==N+1 means mt[N] is not initialized
    
    # initializing the array with a NONZERO seed
    def sgenrand(seed):
      # setting initial seeds to mt[N] using
      # the generator Line 25 of Table 1 in
      # [KNUTH 1981, The Art of Computer Programming
      #    Vol. 2 (2nd Ed.), pp102]
    
      global mt, mti
    
      mt = []
    
      mt.append(seed & 0xffffffffL)
      for i in xrange(1, N + 1):
        mt.append((69069 * mt[i-1]) & 0xffffffffL)
    
      mti = i
    # end sgenrand
    
    
    def genrand():
      global mt, mti
    
      mag01 = [0x0L, MATRIX_A]
      # mag01[x] = x * MATRIX_A  for x=0,1
      y = 0
    
      if mti >= N: # generate N words at one time
        if mti == N+1:   # if sgenrand() has not been called,
          sgenrand(4357) # a default initial seed is used
    
        for kk in xrange((N-M) + 1):
          y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
          mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]
    
        for kk in xrange(kk, N):
          y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
          mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]
    
        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)
        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]
    
        mti = 0
    
      y = mt[mti]
      mti += 1
      y ^= TEMPERING_SHIFT_U(y)
      y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B
      y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C
      y ^= TEMPERING_SHIFT_L(y)
    
      return ( float(y) / 0xffffffffL ) # reals
    
    
    def main():
      sgenrand(4357) # any nonzero integer can be used as a seed
      for j in xrange(100):
          sys.stdout.write('%5f ' % genrand())
          if (j%8) == 7:
              print
      print
    
    main()
    

    不是很pythonic,但很有效

    【讨论】:

    【解决方案2】:

    Mersenne Twister 标准python库使用的实现。你可以在你的 python 发行版的random.py 文件中看到它。

    在我的系统(Ubuntu 9.10)上它位于/usr/lib/python2.6,在Windows 上它应该位于C:\Python26\Lib

    【讨论】:

    • 然而,实际的 Mersenne Twister 代码不在 random.py 中; random.py 指的是用于实际随机数生成的 C 库。
    猜你喜欢
    • 2013-10-13
    • 2014-02-22
    • 1970-01-01
    • 2013-06-11
    • 2017-07-16
    • 1970-01-01
    • 2013-01-22
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多