[JAVA]Random 함수

Random 함수 및 Assertion 사용 예를 같이 보자

@Test
@DisplayName("Random 학습")
void random() {
    Random random = new Random();

    //  int 난수 반환
    for (int i = 0; i < 100; i++) {
        assertThat(random.nextInt())
            .isBetween(Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    // 0 ~ 9 사이의 정수 반환
    for (int i = 0; i < 100; i++) {
        assertThat(random.nextInt(10))
            .isGreaterThanOrEqualTo(0)
            .isLessThan(10);
    }

    // boolean 난수 반환
    for (int i = 0; i < 100; i++) {
        assertThat(random.nextBoolean()).isIn(true, false);
    }

    //  long 난수 반환 (nextFloat, nextDouble, nextBytes 가 더 있다)
    for (int i = 0; i < 100; i++) {
        assertThat(random.nextLong())
            .isBetween(Long.MIN_VALUE, Long.MAX_VALUE);
    }
}

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다