?Java?,???????????,?????SecureRandom
???????????????????,???????????,???????????
???????SecureRandom
???????????:
import java.security.SecureRandom; public class SecureRandomExample { public static void main(String[] args) { // ????SecureRandom?? SecureRandom secureRandom = new SecureRandom(); // ???????? int randomInt = secureRandom.nextInt(); System.out.println("????: " + randomInt); // ?????????????? int randomIntInRange = secureRandom.nextInt(100); System.out.println("???????0?99??: " + randomIntInRange); // ??????????????? byte[] randomBytes = new byte[16]; secureRandom.nextBytes(randomBytes); System.out.println("??????: " + bytesToHex(randomBytes)); } // ??????????????? public static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("x", b)); } return sb.toString(); } }
??????,???????java.security.SecureRandom
?,???main
????????SecureRandom
??????,????nextInt()
?nextBytes()
????????????????,???????????????????????
???,SecureRandom
???????java.util.Random
,?????????????????????,???????????,????????