'2020/07/20'에 해당되는 글 1건

  1. 2020.07.20 [Python] fisher-yates shuffle
Coding/Python 삽질기2020. 7. 20. 23:27
import random

number = [1, 2, 3, 4, 5, 7, 8]

def shuffle(number):
   for i in range (len(number)-1, 0, -1):
       j = random.randint(0, i)
       number[j], number[i] = number[i], number[j]
    
shuffle(number)

 동작을 보면 아래와 같이 변화 한다

[1, 2, 3, 4, 5, 7, 8]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[3, 8, 1, 4, 5, 7, 2]
[3, 8, 1, 4, 5, 7, 2]

* 참고: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

'Coding > Python 삽질기' 카테고리의 다른 글

[Python][AI] Perceptron 예제  (0) 2020.07.28
강아지 나이 계산기  (0) 2020.07.16
[Python] 소인수 분해  (0) 2020.07.10
Posted by chobocho