Swift 난수 생성

Woody·2023년 4월 3일
0

IOS

목록 보기
9/12

난수 생성

1. 정수
Int.random(in: 1...10) 

2. 실수
Float.random(in: 1...10)

3. Boolean
Bool.random()

범위 설정

1. 1<= x <= 5 중 선택
Int.random(in: 1...5)

2. 1<= x < 5 중 선택
Int.random(in: 1..<5)

Array에서 임의의 값 가져오는 법

example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1. 난수 생성하여 가져오기
example[Int.random(in:1...10)]

2. 임의의 원소 선택
example.randomElement()
profile
Developer, Data Engineer

0개의 댓글