Skip to content

Instantly share code, notes, and snippets.

@MaySnow
Created October 9, 2012 06:02
Show Gist options
  • Save MaySnow/3856907 to your computer and use it in GitHub Desktop.
Save MaySnow/3856907 to your computer and use it in GitHub Desktop.
随机产生字符串
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