Java之随机生成各种类型的数据举例
作者:互联网
源码举例:
package com.gxr.imybatisplus.utils; import com.gxr.imybatisplus.entity.TSampleE; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.UUID; import java.util.logging.Logger; public class GenObject { private static final Logger logger = Logger.getLogger(GenObject.class.getName()); public static final String HANZI = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方"; public static final String GENDEL = "男女"; public static final String[] SCHOOLS = {"清华大学", "北京大学", "南京大学", "上海交通大学", "东南大学", "华中科技大学", "武汉大学"}; public static final String[] JOBS = {"商人", "学生", "文职人员", "程序员", "科学家", "医生", "律师", "司机"}; public static final String REMARK = "随着公司项目的持续增多、实施工作持续推进,同时也暴露出很多数据从哪出、数据如何用等问题," + "各项目都在按需定制功能和对接数据,这就给公司带来很大的支出成本"; public static TSampleE genSampleObj(int id, String tableName) { TSampleE sample = new TSampleE(); sample.setTableName(tableName); sample.setId(id); sample.setName(getRandomString(HANZI, 3)); sample.setGender(getRandomString(GENDEL, 1)); // sample.setHeight(175.56F); sample.setHeight(getRandomFloat(150F, 200F)); // sample.setWeight(67.57); sample.setWeight(getRandomDouble(50, 120, 2)); sample.setAge(getRandomInt(16, 100)); int ageTime = sample.getAge() * 365 * 24 * 360; // int类型的数据范围有限 Date date = new Date((System.currentTimeMillis() / 1000 / 10 - ageTime) * 1000 * 10); sample.setBrithday(date); // sample.setSchool("清华大学"); sample.setSchool(getRandomItem(SCHOOLS)); // sample.setJob("商人"); sample.setJob(getRandomItem(JOBS)); sample.setRemarks(getUUID()); sample.setCreateTime(new Timestamp(System.currentTimeMillis())); System.out.println("-- 生成数据: " + sample.toString()); return sample; } public static List<TSampleE> getSampleList(int startNum, int num, String tableName) { List<TSampleE> list = new ArrayList<>(); for (int i = 0; i < num; i++) { TSampleE sample = genSampleObj(startNum + i, tableName); list.add(sample); } return list; } /** * 根据字符串随机获取定长字符串 */ public static String getRandomString(String str, int length) { // String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // String str = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(str.length()); sb.append(str.charAt(number)); } return sb.toString(); } /** * 在字符串数组中随机获取一个值 */ public static String getRandomItem(String[] strs) { Random random = new Random(); int number = 0; number = random.nextInt(strs.length); return strs[number]; } /** * 随机生成一个范围内的整数 */ public static int getRandomInt(int min, int max) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } return (int) (min + Math.random() * (max - min)); } /** * 在指定范围中,随机获取一个double数 * * @param scale :小数点位数 */ public static double getRandomDouble(final double min, final double max, int scale) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } double d = min + ((max - min) * new Random().nextDouble()); d = new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); return d; } /** * 在指定范围中,随机获取一个float数 */ public static float getRandomFloat(final float min, final float max) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } return min + ((max - min) * new Random().nextFloat()); } /** * 获取一个不重复的随机字符串 */ public static String getUUID() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); // System.out.println("原始UUID: " + str); // 去掉"-"符号 String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24); // System.out.println("去掉\"-\"符号: " + temp); return temp; } }
标签:Java,String,min,int,max,各种类型,sample,举例,return 来源: https://www.cnblogs.com/gongxr/p/13937292.html