메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
131 JAVA JDK 제거하기 file 2018.07.09 1409
130 JAVA public, private, protected 정리 2018.07.09 1001
129 JAVA TreeMap 인덱스 값 가져오기 및 Collections.sort 사용하기 2018.07.09 1488
128 JAVA 두개의 문서 파일 비교하기 2018.07.09 2388
127 JAVA 이클립스 인코딩 변경하기 file 2018.07.09 1596
126 JAVA 인코딩을 변경하여 파일 출력하기 (EUC_KR) 2018.07.09 1105
125 JAVA 정규표현식을 이용한 패턴매칭(HTML 제거) 2018.07.09 1164
124 JAVA 현재 시간 구하기 file 2018.07.09 1266
123 JAVA/JSP SQL Injection 해킹 방지를 위한 코딩 2019.01.10 846
122 java에서 이전 URL 알아내기 2021.03.25 691
121 JDK 9에서 eclipse 실행이 안될때 2018.06.05 1596
120 jquery 스크롤(scroll) 따라다니는 배너 레이어 / 위로 버튼 / 화면 상단으로 이동 / scroll layer 이벤트 file 2017.07.05 4057
119 JQuery 자동완성 플러그인 JSDT설치 file 2016.09.19 8896
118 JSON 문자열을 Map 으로 변환하기(Jackson 사용) 2019.01.08 1010
117 JSP, Spring, GMail 메일발송 간단 예제 2016.09.12 32791
116 jstl <c:url value=""> 사용시 ;jsessionid= 붙는 현상 file 2021.03.31 230
115 log4j에서 로그가 출력되지 않는 문제 수정 2021.03.25 405
114 My-SQL 을 이용한 JDBC file 2016.09.21 4282
113 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
112 No bean name '***Service' is defined 오류나는 이유 및 해결방법 file 2016.08.29 4561
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved