乱数

概要

[ edit ]

乱数の生成

[ edit ]

使い方の例

[ edit ]

整数乱数生成

>>> import random
>>> random.seed( 0 )

>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
0
>>> random.randint( 0, 9 )
4
>>> random.randint( 0, 9 )
8
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
4


>>> random.seed( 0 )
>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
0
>>> random.randint( 0, 9 )
4
>>> random.randint( 0, 9 )
8
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
6
>>> random.randint( 0, 9 )
4

>>> random.seed( 1 )
>>> random.randint( 0, 9 )
2
>>> random.randint( 0, 9 )
9
>>> random.randint( 0, 9 )
1
>>> random.randint( 0, 9 )
4
>>> random.randint( 0, 9 )
1
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
7

乱数のシード(種)の設定

>>> import random
>>> import time

>>> random.seed( time.time() )
>>> random.randint( 0, 9 )
4
>>> random.randint( 0, 9 )
3
>>> random.randint( 0, 9 )
5
>>> random.randint( 0, 9 )
3
>>> random.randint( 0, 9 )
1

>>> random.seed( time.time() )
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
0
>>> random.randint( 0, 9 )
1
>>> random.randint( 0, 9 )
7
>>> random.randint( 0, 9 )
6

実数乱数生成

>>> import random
>>> random.seed( 0 )

>>> random.random()
0.8444218515250481
>>> random.random()
0.7579544029403025
>>> random.random()
0.420571580830845
>>> random.random()
0.25891675029296335
>>> random.random()
0.5112747213686085
>>> random.random()
0.4049341374504143

>>> random.seed( 0 )
>>> random.random()
0.8444218515250481
>>> random.random()
0.7579544029403025
>>> random.random()
0.420571580830845
>>> random.random()
0.25891675029296335
>>> random.random()
0.5112747213686085
>>> random.random()
0.4049341374504143

列演算

>>> import random
>>> x = ["a","b","c","d","e","f" ]
>>> random.seed( 0 )

>>> random.choice( x )
'd'
>>> random.choice( x )
'd'
>>> random.choice( x )
'a'
>>> random.choice( x )
'c'
>>> random.choice( x )
'e'
>>> random.choice( x )

>>> import random
>>> x = "abcdef"
>>> random.seed( 0 )

>>> random.choice( x )
'd'
>>> random.choice( x )
'd'
>>> random.choice( x )
'a'
>>> random.choice( x )
'c'
>>> random.choice( x )
'e'

列更新

>>> import random
>>> random.seed( 0 )
>>> x = ["a","b","c","d","e","f" ]

>>> random.shuffle( x )
>>> x
['e', 'c', 'b', 'a', 'f', 'd']

>>> random.shuffle( x )
>>> x
['f', 'e', 'c', 'd', 'b', 'a']

>>> random.shuffle( x )
>>> x
['a', 'd', 'f', 'c', 'e', 'b']

>>> random.shuffle( "abcdef" )

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "P:\0\Conda\lib\random.py", line 275, in shuffle
    x[i], x[j] = x[j], x[i]
TypeError: 'str' object does not support item assignment

>>> import random
>>> random.seed( 0 )
>>> s = "abcdef"
>>> x = [ c for c in s ]
>>> x
['a', 'b', 'c', 'd', 'e', 'f']

>>> random.shuffle( x )
>>> "".join(x)
'ecbafd'

>>> random.shuffle( x )
>>> "".join(x)
'fecdba'

>>> random.shuffle( x )
>>> "".join(x)
'adfceb'

>>> random.shuffle( x )
>>> "".join(x)
'bacdfe'

SoftEng: Python/Lang/DataType/Numeric/Random/Contents (last edited 2018-10-11 04:13:00 by TadashiIijima)