- 자바 API 7 [Random과 SecureRandom의 차이]

 

java.util.Random 클래스는 난수를 생성할 때 seed값으로 '시간'을 이용한다. 
따라서, 각각 동일한 시간에 Random 클래스를 이용하여 난수를 생성하게 되면,
동일한 값이 리턴된다. [ deterministic(결정론적)]

java.security.SecureRandom클래스는 이와 다르게 non-deterministic(비결정론적) output을 생산한다.
예측할수 없는 seed를 이용하여 난수를 생성하기 때문이다.

 

참고 : API document에서 발췌

 

[Unlike the java.util.Random class, the java.security.SecureRandom class must produce non-deterministic output on each call. What that means is, in case of java.util.Random, if you were to recreate an instance with the same seed each time you needed a new random number, you would essentially get the same result every time. However, SecureRandom is guaranteed to NOT do that - so, creating a single instance or creating a new one each time does not affect the randomness of the random bytes it generates


Additionally, SecureRandom must produce non-deterministic output and therefore it is required that the seed material be unpredictable and that output of SecureRandom be cryptographically strong sequences as described in RFC 1750: Randomness Recommendations for Security.]

 

 

다음은 각각 Random과 secureRandom 을 이용하여 1부터 100이하의 난수를 생성하는 예제이다.

 

 

'Java' 카테고리의 다른 글

자바 [getOrDefault, putIfAbsent]  (0) 2020.01.22
자바 예외처리 [Exception]  (0) 2020.01.22
자바 API 6 [HashMap, HashSet, Iterator]  (0) 2020.01.21
자바 API 5 [InputStream,OutputStream]  (0) 2020.01.21
자바 API 4 [StringTokenizer]  (0) 2020.01.20

+ Recent posts