메뉴 건너뛰기

조회 수 766 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

자바 랜덤 함수를 사용한 (JAVA Random)무작위 난수 생성 방법


첫번째 방법은 Math 클래스의 random 메소드를 사용하는 방법입니다.

0~1 사이의 double형 난수를 리턴하여 *10을 해주고 int형으로 변환 후 a, b, c, d 라는 이름을 가진 int형 변수에 담아 출력하는 예제입니다.



두번째는 Random 클래스를 사용하는 방법입니다.

boolean nextBoolean() float nextFloat()등등 random.nextInt(100)을 입력하시면 0~100사이의 난수가 발생합니다.

import java.util.Random;

  

public class h5bak_random {

    public static void main(String[] args) {

        int a = (int) (Math.random()*10);

        int b = (int) (Math.random()*10);

        int c = (int) (Math.random()*10);

        int d = (int) (Math.random()*10);

        int e = (int) (Math.random()*10);

        System.out.println(a+","+b+","+c+","+d+","+e);

          

        System.out.println("=========================");

          

        Random random = new Random();

        int f = random.nextInt(10);

        int g = random.nextInt(10);

        int h = random.nextInt(10);

        int i = random.nextInt(10);

        int j = random.nextInt(10);

          

        System.out.println(f+","+g+","+h+","+i+","+j);

    }

}



출력결과



List of Articles
번호 제목 날짜 조회 수
51 이클립에서 Javadoc 생성시 unmappable character for encoding MS949 에러가 발생할때 file 2019.03.05 730
50 익명클래스 2016.09.13 3143
49 인터페이스 2016.09.13 3089
48 인터페이스와 다형성 2016.09.13 3535
47 자바 - 공백 문자 제거하기 (trim, replaceAll) file 2021.03.31 173
46 자바 extends Thread, implements Runnable 차이 file 2023.02.15 77
45 자바 extends, implements 차이점 알아보기 file 2023.02.15 75
44 자바 int 값 자리수 구하기 file 2023.02.15 74
43 자바 JXL 엑셀파일을 읽어 배열리턴 : JAVA EXCEL ArrayList 2016.12.09 4389
42 자바 Statement PreparedStatement 차이 알아보기 file 2023.02.15 53
41 자바 String Class 문자열 처리 함수에 대한 정리 2021.03.31 106
40 자바 this, super 차이 알아보기 file 2023.02.15 72
39 자바 Thread dump file 2023.02.15 58
38 자바 XML 제어 라이브러리 XStream : JAVA 2016.12.09 4917
37 자바 객체화(인스턴스화) 알아보기 file 2023.02.15 69
36 자바 날짜 포맷 변환 방법 file 2018.06.21 1425
35 자바 다양한 형변환. 그리고 아스키 코드 String char int : JAVA 2016.12.09 4188
34 자바 대소문자 확인하는 방법 file 2023.02.15 130
» 자바 랜덤 함수(Java random) file 2019.03.05 766
32 자바 메소드(Method)란 무엇인가? file 2023.02.15 78
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved