[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); } }
최신 댓글