
random을 사용하기 전에
import random
을 통해 불러와야한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import turtle import random t= turtle.Turtle() t.shape("turtle") for a in range(5): l =random.randint(1,100) t.fd(l) angle = random.randint(0,360) t.right(angle) items = [1, 2, 3, 4, 5, 6, 7] mun = 'abcdefghij' b= random.choice('abcdefghij') # 하나의 랜덤한 항목 선택 random.shuffle(items) # 랜덤하게 리스트 항목들을 섞는 기능임 # 섞기만 할뿐 그 배열을 의미하지 않음. random.shuffle(mun) print("random choice:",b) print("random shuffle:",items) #print("random shuffle:",mun) #문자열을 shuffle 할 수 없음! 리스트만! ab = [1,2,3,4,] print(ab) | cs |
import만 하면 random을 사용할 수 있다.
randint(1,100) # 1부터 100이하의 수 중 랜덤으로 돌려준다. 100도 포함이다!
randchoice("123456") # 문자열중 랜덤으로 1개를 선택함
randomshuffle # f가 두개임. 문자열은 셔플 불가, 배열이 가능하다.
'Python > Python Concept' 카테고리의 다른 글
[Python] 함수정의 : def (0) | 2018.01.11 |
---|---|
[Python] while (0) | 2018.01.11 |
[Python] for문 (0) | 2018.01.11 |
[Python] 터틀 관련 함수정리 (0) | 2018.01.10 |
[Python] 문자열 함수 (0) | 2018.01.07 |