java生成随机数

方法一:

/**

* 生成随机数

* @author [email protected]

* @return

*/

public synchronized static String getChargeSequenceNum(String pre){

StringBuffer sequenceNum = new StringBuffer();

Date dateTime = new Date();

Random rand = new Random();

int random = rand.nextInt(99999);

while(random < 10000){

random = rand.nextInt(99999);

}

try{

sequenceNum.append(pre);//充值

sequenceNum.append(DateUtil.date2Str("yyyyMMddHHmmssSSS", dateTime));//日期

sequenceNum.append(random);//随机数

}catch(Exception e){

e.printStackTrace();

}

return sequenceNum.toString();

}

方法二:

 System.Random random = new Random(DateTime.Now.Millisecond);
       public static string GetDateTimeStr()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + Rand.Number(2,true);
        }
        public static string Number(int Length, bool Sleep)
        {
            if (Sleep)
                System.Threading.Thread.Sleep(3);
            string result = "";
            System.Random random = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < Length; i++)
            {
                result += random.Next(10).ToString();
            }
            return result;
        } 

你可能感兴趣的:(java)