Java로 난수 생성


Math.random


  • Math.random : 0.0 이상 1.0 미만의 랜덤 값 반환
  • (int)(Math.*random*() * 100) + 1 : 1 이상 100 이하의 정수 반환



최대값, 최소값 설정


  • 최대값 설정 : Math.random() * 최대값
  • 최소값 설정 : Math.random() + 최소값
  • 최대값 및 최소값 설정 : (Math.random() * (최대값 - 최소값)) + 최소값



12자리 난수 생성하려면?


(long)((Math.*random*() * (Math.*pow*(10, 11) - 11111111111L)) + Math.*pow*(10, 11))



java.util.Random


  • nextInt() : 32bit로 표현 가능한 Int형의 난수 생성

      Random random = new Random();
      System.out.println(random.nextInt());
    
  • 이 외의 자료형의 난수 생성

    자료형 상세
    nextDouble() 0과 1 사이의 Double 난수
    nextFloat() 0과 1 사이의 Float 난수
    nextLong() Long 타입의 난수
    nextBoolean() True or False 무작위로



최대값 설정


ex. 100 미만의 정수 난수 생성

int bound = 100;
		
Random random = new Random();
System.out.println(random.nextInt(bound));


💡 Random의 초기값(seed)?

  • Random 클래스의 생성자는 인자로 seed를 받고, 동일한 seed를 입력하면 항상 동일한 난수를 생성
Random random1 = new Random(10);
Random random2 = new Random(10);
		
System.out.println(random1.nextInt()); // -1157793070
System.out.println(random2.nextInt()); // -1157793070



Apache commons-math3 라이브러리


  • Gradle 프로젝트의 build.gradle에 dependency cnrk

      dependencies {
        compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
        ...
      }
    
  • 난수 생성

      int randomInt = new RandomDataGenerator().getRandomGenerator().nextInt();
    
    • Double은 nextDouble(), Long은 nextLong()
  • 범위 지정하여 난수 생성

      int leftLimit = 100;
      int rightLimit = 120;
      int randomInt = new RandomDataGenerator().nextInt(leftLimit, rightLimit);
    



그런데,, DB에서 난수 중복 체크..?


방법 1. 미리 난수를 DB에 저장하고 빼서 쓰기

방법 2. 시간 + 아이피 또는 php 세션값을 조합 (중복 확률 낮음)



참고 자료


Tags:

Categories:

Updated:

Leave a comment