Javaランダム生成定長純数字または数字アルファベット混合数
3003 ワード
package randomdata;
import java.util.Random;
public class RandomCharData {
// int , ASCII
public static void main(String[] args) {
//
for(int i=0;i<15;i++)
createData(20);
System.out.println("---------------");
//
for(int i=0;i<15;i++)
createRandomCharData(20);
}
//
//0~9 ASCII 48~57
//A~Z ASCII 65~90
//a~z ASCII 97~122
public static void createRandomCharData(int length)
{
StringBuilder sb=new StringBuilder();
Random rand=new Random();//
Random randdata=new Random();
int data=0;
for(int i=0;i<length;i++)
{
int index=rand.nextInt(3);
// ,
switch(index)
{
case 0:
data=randdata.nextInt(10);// 0~9
sb.append(data);
break;
case 1:
data=randdata.nextInt(26)+65;// 65~90
sb.append((char)data);
break;
case 2:
data=randdata.nextInt(26)+97;// 97~122
sb.append((char)data);
break;
}
}
String result=sb.toString();
System.out.println(result);
}
//
public static void createData(int length) {
StringBuilder sb=new StringBuilder();
Random rand=new Random();
for(int i=0;i<length;i++)
{
sb.append(rand.nextInt(10));
}
String data=sb.toString();
System.out.println(length+" random data: "+data);
}
}
結果は次のとおりです.
97862483216351602095
70514049428709111734
58909883924816310266
57687118857245129003
61026032192487969045
24071314312917474726
33994529915727126720
96782018153746273742
63798758707067214215
07076213273633627868
48227106476502535792
59944228129822555128
20025042941676211212
70116211781380045731
88967160116193801569
---------------
0Qa0cZYe6YyMf4zrLEzEJqK6f
MwwDl6Kan92RseOPWJuf6WpnF
0H1249B0Y7qoGmbj0ZWeCliId
PIsRRAS99XIbPwBqCdV7BUt7e
X6R5cotl638K83c4fowNv0S3j
03p7YzBA0q2OlB70pL4MTQf5P
7x5Yd57SOYGoTS9ABi8RA2F5y
1XVK9wrhrFxINd4gpi4Ip05Hg
r53YoB2aBgeP2i4061A2CMNK2
X1R3VWi7423VSs0j0u6R7uMy7
uCQ19SGe4U19V9U4XVqB5QDmc
2OaJ1O3JC52617kMTH2yK9yyJ
kM85MalnsR8T299yR8B0bP5DH
rAApB65VXpj4qxp0gEj4Oh34I
3k88S216XWbK83XaCTnvin02f