dev/flutter
[Dart] Random 클래스 사용 + Color
Neander
2024. 9. 21. 15:17
랜덤하게 숫자를 받아야 한다. 그것을 '난수'라고 부른다.
1부터 10까지 랜덤하게 숫자를 받고 싶은 경우 Random을 사용한다.
Import
import 'dart:math';
Create Instance
var random = Random();
// 1 ~ 10
int result = random.nextInt(10) + 1;
+1을 해주지 않으면 0부터 9까지 숫자를 갖게 된다.
double형태로 받고 싶은 경우 (0.0 ~ 1.0)
double resultDouble = random.nextDouble();
Boolean형태로 받고 싶은 경우
bool resultBool = random.nextBool();
Color로 받고 싶은 경우
// Random from 'dart:math'
Color currentColor = Color((Random().nextDouble() * 0xFFFFFF).toInt());