[개발메모] Math.random()

package object;

public class Random {

	public static void main(String[] args) {
		// Math.random()
		
		int n = (int) (Math.random()*10); //0~9 까지의 난수 발생 ex.7
		System.out.println(n);
		int k = (int) (Math.random()*45+1); //1~45까지의 난수 발생 ex.37
		System.out.println(k);
	}

}

백준 문제를 풀다가 난수를 발생시키는 법을 까먹어서 쓰게되었다.

자바에서 난수를 발생시키는 함수는 Math.random() 이다.

Math.random() 은 double형의 0.xxxxxx~0.9xxxxx 까지의 난수를 반환시킨다.

다른 수, 예를 들어, 0~45까지의 를 만들어내려면, (int) Math.random() * 45 를 해주면 된다.