2012年4月11日 星期三

[Python Std Library] Numeric and Math Modules : random — Generate pseudo-random numbers


翻譯自 這裡
Preface :
在 random 模組透過類別 SystemRandom 呼叫系統函數 os.urandom() 來產生亂數. 接著來看看上面提供的函數 :
random.seed([x])
Changed in version 2.4: formerly, operating system resources were not used.
Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

random.getstate()
New in version 2.1.
Changed in version 2.6: State values produced in Python 2.6 cannot be loaded into earlier versions.

Return an object capturing the current internal state of the generator. This object can be passed to setstate() to restore the state.

random.setstate(state)
New in version 2.1.
state should have been obtained from a previous call to getstate(), and setstate() restores the internal state of the generator to what it was at the time setstate()was called.

random.jumpahead(n)
New in version 2.1.
Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many steps.

Change the internal state to one different from and likely far away from the current state. n is a non-negative integer which is used to scramble the current state vector. This is most useful in multi-threaded programs, in conjunction with multiple instances of the Random class: setstate() or seed() can be used to force all instances into the same internal state, and then jumpahead() can be used to force the instances’ states far apart.

random.getrandbits(k)
New in version 2.4.
Returns a python long int with k random bits. This method is supplied with the MersenneTwister generator and some other generators may also provide it as an optional part of the API. When available, getrandbits() enables randrange() to handle arbitrarily large ranges.

底下的函數讓你可以任意產生整數的亂數 :
random.randrange([start], stop[, step])
New in version 1.5.2.
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

random.randint(ab)
Return a random integer N such that a <= N <= b.

底下函數提供更彈性用法 :
random.choice(seq)
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

random.shuffle(x[, random])
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the functionrandom().
>>> list = [1, 1, 2, 3, 4, 5, 6]
>>> random.shuffle(list, random.random)
>>> list
[2, 3, 6, 1, 4, 1, 5]

random.sample(populationk)
New in version 2.3.
Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.
>>> list = [1, 2, 3, 4, 5, 6]
>>> random.sample(list, 3)
[6, 5, 4]

更多如產生浮點數亂數 (random.random() : Return the next random floating point number in the range [0.0, 1.0).), 高斯分布 (random.gauss(musigma)) 等相關應用, 可以參考The Python Standard Library.

Example :
來看一些使用範例 :
>>> random.random() # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10) # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2) # Even integer from 0 to 100
26
>>> random.choice('abcdefghij') # Choose a random element
'c'

>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]

>>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements
[4, 1, 5]

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...