Created
October 9, 2012 06:02
-
-
Save MaySnow/3856907 to your computer and use it in GitHub Desktop.
随机产生字符串
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit; | |
import java.util.Random; | |
import java.util.TimeZone; | |
import com.kaishengit.pojo.User; | |
import com.kaishengit.util.DateUtil; | |
import hirondelle.date4j.DateTime; | |
public class Test { | |
private static String ssource = "abcdefghijklmnopqrstuvwxyz" + "0123456789"; | |
private static char[] src = ssource.toCharArray(); | |
/** | |
* 产生随机字符串 | |
* | |
* @param length | |
* @return | |
*/ | |
public static String getCode(int length) | |
{ | |
Random r = new Random(); | |
char[] buf = new char[length]; | |
int rnd; | |
for (int i = 0; i < length; i++) | |
{ | |
rnd = Math.abs(r.nextInt()) % src.length; | |
buf[i] = src[rnd]; | |
} | |
return new String(buf); | |
} | |
public static void main(String[] args) { | |
String str = getCode(3); | |
System.out.println(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment