package com.winsun.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * 随机数工具类
 */
public class RandomUtil {

    /**
     * 获取更安全的随机数
     * @return
     */
    public static double getSecrityRandom() {
        SecureRandom random = null;
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
            return random.nextDouble();
        }catch (Exception e) {
        }
        return 0.0;
    }

    /**
     * 获取int随机数
     * @return
     */
    public static int getSecrityRandomInt(Integer i) {
        SecureRandom random = null;
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
            return random.nextInt(i);
        }catch (Exception e) {
        }
        return 0;
    }

    /*	组成20个随机数的订单号   */
    public static String RandomNumber(int length){
        String str="1234567890abcdefghijklmnopqrstuvwxyz";
        try {
            SecureRandom random = SecureRandom.getInstance(str);

            StringBuffer sb=new StringBuffer();
            for(int i=0;i<length;i++){

                int number =random.nextInt(36);

                sb.append(str.charAt(number));
            }
            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取纯数字随机数
     * @param length
     * @return
     */
    public static String ramdomNum(Integer length){
        String str="1234567890";
        try{
            SecureRandom random = SecureRandom.getInstance(str);
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<length;i++){

                int number =random.nextInt(10);

                sb.append(str.charAt(number));
            }

            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
