메뉴 건너뛰기

조회 수 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 JAVA 현재 시간 구하기 file 2018.07.09 1266
50 예외처리 / 예외발생 file 2018.09.21 934
49 직렬화 / 역직렬화 file 2018.09.21 951
48 시간관련 클래스 file 2018.09.21 1186
47 JSON 문자열을 Map 으로 변환하기(Jackson 사용) 2019.01.08 1010
46 Apache Commons HttpClient 3.x 로 Http 서버에 파일 전송하기 file 2019.01.08 1136
45 Java : JSOUP 를 이용, html에서 소스, 링크경로 추출후 절대 경로로 바꾸기 2019.01.08 1166
44 JAVA/JSP SQL Injection 해킹 방지를 위한 코딩 2019.01.10 846
43 기본적인 스크립트 보안 2019.01.16 6044
42 변환 (문자, 숫자, KSC5601.....) 2019.01.16 1281
41 A java Runtime Environment(JRE) or Java Development Kit(JDK) must be ~~~~ 하면서 이클립스가 실행안될때. file 2019.03.05 789
40 이클립스 html, js 등등의 파일에서 에러표시 지우기 2019.03.05 1445
39 이클립에서 Javadoc 생성시 unmappable character for encoding MS949 에러가 발생할때 file 2019.03.05 730
38 이클립에서 FTP 접속하면서 Operation failed. File system input or output error 가 날때 file 2019.03.05 878
37 이클립스에서 같은 파일을 여러 편집창으로 띄우기 file 2019.03.05 677
» 자바 랜덤 함수(Java random) file 2019.03.05 766
35 국제 시간에 따른 날짜 출력 2020.06.29 121
34 XML to JSON , JSON to Map 2020.06.29 262
33 사용자의 IP를 가져오기 (IPv4) 2020.06.29 1693
32 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 256
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved