先说随机的int吧
用Random类里面的nextInt(int max);里面的参数就是上限String也是比较好处理的
char []ch={'a','b','c',.......,'z'}
然后用随机整数取其中的字母,然后再组合起来就是一个随机的String了
用Random类里面的nextInt(int max);里面的参数就是上限String也是比较好处理的
char []ch={'a','b','c',.......,'z'}
然后用随机整数取其中的字母,然后再组合起来就是一个随机的String了
int num = (int) (Math.random() * 100) + 1;
num = num%4 + 6; // 表示字符串的长度为6-10,这里的长度你可以用参数来控制
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++) {
int j = (int) (Math.random() * 26);
char c = (char) ('a' + j);
sb.append(c);
}
return sb.toString();
}然后在main()里面调用
for(int i=1;i<=10000;i++)
{ // 获取10000个随机字符串
System.out.println(getRandString());
}
int num = (int) (Math.random() * 100) + 1;
这个是获取1-100之间的随机树
生成10000条数据,
每条数据包括序号,姓名(6-10长度的字符串),性别(0,1),类别(1,2),office(1,2,3,4,5)
生成的文件放在F:\abcd.txt
呵呵,enjoy itpackage random;import java.io.RandomAccessFile;
import java.io.File;
import java.util.Random;public class radom {
public radom() {
} public static String getRandString() {
int num = (int) (Math.random() * 100) + 1;
num = num%4 + 6;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++) {
int j = (int) (Math.random() * 26);
char c = (char) ('a' + j);
sb.append(c);
}
return sb.toString();
} public static void main(String args[]) {
int sex;
String name = "";
int office;
int kind;
File f = new File("F://abcd.txt");
try {
RandomAccessFile raf = new RandomAccessFile(f, "rw");
for (int i = 1; i <= 100000; i++) {
int num = (int) (Math.random() * 100) + 1;
sex = num % 2;
name = getRandString();
office = num % 5 + 1;
int num2 = (int) (Math.random() * 100) + 1;
kind = num2 % 2;
String s = Integer.toString(i) + " " + Integer.toString(sex) + " " +
name + " " + Integer.toString(office) + " " + Integer.toString(kind) +
"\r\n";
raf.seek(raf.length());
raf.writeBytes(s); }
}
catch (Exception e) {
} }
}