메뉴 건너뛰기

조회 수 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 자바 String Class 문자열 처리 함수에 대한 정리 2021.03.31 106
130 자바에서 문자열 비교 시 == 가 아닌 equals를 써야하는 이유 file 2023.02.15 114
129 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 119
128 국제 시간에 따른 날짜 출력 2020.06.29 121
127 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 122
126 자바 대소문자 확인하는 방법 file 2023.02.15 130
125 HashMap 사용하기 file 2021.03.31 134
124 [객체 지향 언어의 이해] 업캐스팅과 다운캐스팅 file 2021.03.31 157
123 자바 - 공백 문자 제거하기 (trim, replaceAll) file 2021.03.31 173
122 jstl <c:url value=""> 사용시 ;jsessionid= 붙는 현상 file 2021.03.31 230
121 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 256
120 XML to JSON , JSON to Map 2020.06.29 262
119 log4j에서 로그가 출력되지 않는 문제 수정 2021.03.25 405
118 이클립스에서 같은 파일을 여러 편집창으로 띄우기 file 2019.03.05 677
117 java에서 이전 URL 알아내기 2021.03.25 690
116 이클립에서 Javadoc 생성시 unmappable character for encoding MS949 에러가 발생할때 file 2019.03.05 730
» 자바 랜덤 함수(Java random) file 2019.03.05 766
114 A java Runtime Environment(JRE) or Java Development Kit(JDK) must be ~~~~ 하면서 이클립스가 실행안될때. file 2019.03.05 789
113 JAVA/JSP SQL Injection 해킹 방지를 위한 코딩 2019.01.10 846
112 이클립에서 FTP 접속하면서 Operation failed. File system input or output error 가 날때 file 2019.03.05 878
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved