티스토리 뷰

공부

[Java] RandomUtils

승가비 2022. 1. 10. 20:52
728x90
package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Random;

public class RandomUtils {
   private static class SHA256 {
      public static String encrypt(String text) {
         MessageDigest md = null;
         try {
            md = MessageDigest.getInstance("SHA-256");
         } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
         }
         md.update(text.getBytes());

         return bytesToHex(md.digest());
      }

      private static String bytesToHex(byte[] bytes) {
         StringBuilder builder = new StringBuilder();
         for (byte b : bytes) {
            builder.append(String.format("%02x", b));
         }
         return builder.toString();
      }
   }

   private static final Random random = new Random();

   public static Object random(String type) {
      switch (type) {
         case "string":
            return SHA256.encrypt(String.valueOf(random.nextDouble()));
         case "integer":
            return random.nextInt();
         case "long":
            return random.nextLong();
         case "double":
            return random.nextDouble();
         default:
            return null;
      }
   }

   public static Map<String, Object> random(Map<String, Object> map) {
      for (String k : map.keySet()) {
         map.put(k, random((String) map.get(k)));
      }

      return map;
   }
}

https://psychoria.tistory.com/761

 

[Java] 자바 난수 (Random Number) 생성 방법

난수(랜덤 넘버) 생성은 프로그래밍에서 자주 사용되는 기능입니다. 확률을 적용하기 위해 난수를 사용할 수 있습니다. 로또 번호를 생성하는 프로그램을 작성할 때 난수 생성 방법을 활용할 수

psychoria.tistory.com

 

728x90

'공부' 카테고리의 다른 글

[MySQL] Types  (0) 2022.01.10
[Java] JSON Object  (0) 2022.01.10
[MySQL] create user  (0) 2022.01.10
[Java] SHA256  (0) 2022.01.10
[Java] resource  (0) 2022.01.10
댓글