본문으로 바로가기

파이썬 랜덤 모듈 사용하기

category Language/Python 2020. 6. 18. 21:40
728x90

random()

random()은 0과 1사이의 실수를 반환한다. (0 <= x < 0)

import random

print(random.random.__doc__)
for i in range(10):
    ran = random.random()
    print(f'[{i}] {ran}')
더보기
random() -> x in the interval [0, 1).
[0] 0.5929115143771286
[1] 0.6740897798629769
[2] 0.7273050145961745
[3] 0.356795913096035
[4] 0.6579894236795625
[5] 0.1649985077423547
[6] 0.32859035067036346
[7] 0.9291320533374849
[8] 0.3958371558951975
[9] 0.17532426241025212

 

randint(a, b)

randint()는 a와 b사이의 정수를 반환한다. (a <= x <= b)

print(random.randint.__doc__)
for i in range(10):
    ran = random.randint(0, 10)
    print(f'[{i}] {ran}')
더보기
Return random integer in range [a, b], including both end points.
[0] 5
[1] 6
[2] 10
[3] 4
[4] 1
[5] 8
[6] 10
[7] 0
[8] 0

 

randrange(start, end, step)

randrange()는 start와 end 사이의 정수를 반환한다. (start <= x*step < end)

print(random.randrange.__doc__)
for i in range(10):
    ran = random.randrange(10)
    print(f'[{i}] {ran}')
print()
for i in range(10):
    ran = random.randrange(0, 10)
    print(f'[{i}] {ran}')
print()
for i in range(10):
    ran = random.randrange(0, 10, 2)
    print(f'[{i}] {ran}')
더보기
Choose a random item from range(start, stop[, step]).

        This fixes the problem with randint() which includes the
        endpoint; in Python this is usually not what you want.


1st [0] 2
1st [1] 3
1st [2] 9
1st [3] 0
1st [4] 6
1st [5] 8
1st [6] 3
1st [7] 4
1st [8] 2
1st [9] 2

2nd [0] 6
2nd [1] 3
2nd [2] 6
2nd [3] 8
2nd [4] 9
2nd [5] 5
2nd [6] 5
2nd [7] 0
2nd [8] 7
2nd [9] 4

3rd [0] 8
3rd [1] 6
3rd [2] 8
3rd [3] 4
3rd [4] 0
3rd [5] 8
3rd [6] 4
3rd [7] 8
3rd [8] 2
3rd [9] 2

 

choice(seq)

choice()는 sequence로 부터 임의의 원소를 하나 추출한다.

print(random.choice.__doc__)
colors = ['red', 'yellow', 'green', 'blue', 'black', 'pink']
for i in range(10):
    choice = random.choice(colors)
    print(f'[{i}] {choice}')
더보기
Choose a random element from a non-empty sequence.
[0] pink
[1] black
[2] red
[3] green
[4] yellow
[5] blue
[6] green
[7] red
[8] black
[9] pink

 

sample(seq, k)

choice()는 sequence로 부터 k개의 원소를 임의로 추출한다.

print(random.sample.__doc__)
colors = ['red', 'yellow', 'green', 'blue', 'black', 'pink']
for i in range(10):
    k = random.randint(1, len(colors) - 1)
    choice = random.sample(colors, k)
    print(f'[{i}] {choice}')
더보기
Chooses k unique random elements from a population sequence or set.

        Returns a new list containing elements from the population while
        leaving the original population unchanged.  The resulting list is
        in selection order so that all sub-slices will also be valid random
        samples.  This allows raffle winners (the sample) to be partitioned
        into grand prize and second place winners (the subslices).

        Members of the population need not be hashable or unique.  If the
        population contains repeats, then each occurrence is a possible
        selection in the sample.

        To choose a sample in a range of integers, use range as an argument.
        This is especially fast and space efficient for sampling from a
        large population:   sample(range(10000000), 60)

[0] ['blue']
[1] ['blue', 'green', 'red', 'yellow', 'pink']
[2] ['green']
[3] ['red', 'yellow', 'pink', 'green']
[4] ['blue']
[5] ['blue', 'green', 'yellow', 'black', 'red']
[6] ['black', 'red']
[7] ['pink']
[8] ['green', 'blue', 'pink']
[9] ['red']

 

shuffle(seq)

shuffle()은 sequence를 랜덤하게 섞어준다.

print(random.shuffle.__doc__)
colors = ['red', 'yellow', 'green', 'blue', 'black', 'pink']
for i in range(10):
    random.shuffle(colors)
    print(f'[{i}] {colors}')
더보기
Shuffle list x in place, and return None.

        Optional argument random is a 0-argument function returning a
        random float in [0.0, 1.0); if it is the default None, the
        standard random.random will be used.

[0] ['black', 'blue', 'pink', 'yellow', 'red', 'green']
[1] ['pink', 'blue', 'yellow', 'green', 'red', 'black']
[2] ['yellow', 'pink', 'black', 'red', 'green', 'blue']
[3] ['pink', 'black', 'blue', 'red', 'yellow', 'green']
[4] ['pink', 'black', 'blue', 'red', 'green', 'yellow']
[5] ['pink', 'blue', 'green', 'yellow', 'black', 'red']
[6] ['pink', 'red', 'yellow', 'black', 'blue', 'green']
[7] ['red', 'black', 'green', 'pink', 'blue', 'yellow']
[8] ['red', 'blue', 'green', 'black', 'pink', 'yellow']
[9] ['red', 'green', 'pink', 'black', 'yellow', 'blue']