메뉴 건너뛰기

조회 수 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 회원가입 폼(form)으로 보는 Validator 구현하는 방법 file 2016.08.29 7108
130 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3846
129 Database Connections 생성하기 (오라클) file 2016.08.29 4072
128 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
127 전자정부 표준프레임워크 설치하기 file 2016.08.29 4250
126 간단한 개인홈페이지 만들어보기 file 2016.08.29 4751
125 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
124 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
123 전자정부표준프레임워크 - 설치 file 2016.09.02 6277
122 Spring Boot 프로젝트 생성 file 2016.09.02 4160
121 전자정부프레임워크 v2.5, v2.6 오라클 세팅하기 file 2016.09.12 5109
120 JSP, Spring, GMail 메일발송 간단 예제 2016.09.12 32791
119 변수의 종류 2016.09.13 3978
118 클래스멤버와 인스턴스멤버간의 참조와 호출 2016.09.13 3346
117 클래스 메서드와 인스턴스 메서드 2016.09.13 3720
116 static 2016.09.13 3354
115 컬렉션 프레임워크 file 2016.09.13 3705
114 제네릭 file 2016.09.13 3297
113 파일I/O 개요 file 2016.09.13 3507
112 바이트 기반의 스트림 file 2016.09.13 3763
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved