Java生成随机数代码演示
import java.util.Random; public class RandomCode { public String getrandomCode(){ Random random = new Random(); StringBuffer randomCode = new StringBuffer(); int countCode = 4; char[] codeSequence = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; for(int i=0;i<countCode;i++){ String strRand = String.valueOf( codeSequence[random.nextInt(codeSequence.length)]); randomCode.append(strRand); } return randomCode.toString(); } public static void main(String[] args) { RandomCode rd = new RandomCode(); String str = rd.getrandomCode(); System.out.println(str); } }