Java随机数据生成器

Java随机数据生成器

一、概述

简单易用的随机数据生成器。一般用于开发和测试阶段的数据填充、模拟、仿真研究、演示等场景。

可以集成到各种类型的java项目中使用。

二、优点

  • 非常轻量级(不到1M),容易集成,无需过多第三方依赖
  • 简单方便,无需编写冗余代码
  • 生成的随机数据比较接近真实数据

三、支持的随机数据类型

3.1 日期时间

  • 随机日期
  • 随机时间
  • 随机时间戳
  • 随机时区名称

3.2 地理

  • 随机省份和城市
  • 随机国家或地区(基于ISO 3166-1标准)
  • 虚拟经纬度
  • 随机邮编
  • 虚拟联系地址
  • 随机固话区号
  • 虚拟固话号码

3.3 互联网

  • 虚拟邮箱地址
  • 虚拟域名
  • 虚拟静态URL
  • 虚拟IPv4地址
  • 虚拟IPv6地址
  • 虚拟网卡MAC地址
  • 随机强密码
  • 随机User-Agent
  • 随机端口号
  • 虚拟app名称
  • 虚拟app bundle id
  • 虚拟app版本号

3.4 个人信息

  • 虚拟中文人名
  • 虚拟英文人名
  • 虚拟身份证号码
  • 虚拟网络昵称
  • 虚拟拼音网络昵称
  • 虚拟手机号码
  • 虚拟QQ号码
  • 虚拟非主流QQ网名
  • 随机民族名称

3.5 教育

  • 随机学历
  • 虚拟小学名称、年级、班级
  • 虚拟中学名称、年级、班级
  • 随机高校及专业名称

3.6 金融

  • 随机股票名称+股票代码
  • 虚拟日K线数据
  • 随机开放式基金名称+基金代码
  • 随机货币信息
  • 虚拟银行卡(借记卡及信用卡)号码

3.7 体育

  • 随机六大足球联赛球队名称
  • 随机篮球联赛球队名称
  • 随机国家及地区的足球代表队名称

3.8 其它

  • 随机数字
  • 随机汉字
  • 随机成语
  • 随机唐诗
  • 虚拟车牌号
  • 随机热门手机型号
  • 随机RGB颜色值
  • 随机HEX颜色值
  • 随机中文短句
  • 随机英文文本
  • 虚拟企业及部门名称
  • 随机营销号文案
  • 随机ISBN
  • 随机EAN商品编码
  • 随机行业分类
  • 随机统一社会信用代码
  • 数据生成工具

四、配置依赖

<dependency>
    <groupId>com.apifan.commongroupId>
    <artifactId>common-randomartifactId>
    <version>1.0.19version>
dependency>

五、基础用法

5.1 统一入口

//地区类虚拟数据
AreaSource areaSource = RandomSource.areaSource();

//日期时间类虚拟数据
DateTimeSource dateTimeSource = RandomSource.dateTimeSource();

//教育类虚拟数据
EducationSource educationSource = RandomSource.educationSource();

//金融类虚拟数据
FinancialSource financialSource = RandomSource.financialSource();

//互联网信息类虚拟数据
InternetSource internetSource = RandomSource.internetSource();

//数字类虚拟数据
NumberSource numberSource = RandomSource.numberSource();

//个人类虚拟数据
PersonInfoSource personInfoSource = RandomSource.personInfoSource();

//体育竞技类虚拟数据
SportSource sportSource = RandomSource.sportSource();

//语言文字类虚拟数据
LanguageSource languageSource = RandomSource.languageSource();

//其它杂项虚拟数据
OtherSource otherSource = RandomSource.otherSource();

5.2 随机数字

5.2 随机数字

// 生成1个1~101(不含)之间的随机整数
int anInt = RandomSource.numberSource().randomInt(1, 101);
System.out.println("anInt = " + anInt);

// 生成8个1~101(不含)之间的随机整数
int[] anInt2 = RandomSource.numberSource().randomInt(1, 101, 8);
List<Integer> anInt2s = IntStream.of(anInt2).mapToObj(i -> i).collect(Collectors.toList());
System.out.println("anInt2s = " + anInt2s);

// 生成1个10000000000~20000000001(不含)之间的随机长整数
long aLong = RandomSource.numberSource().randomLong(10000000000L, 20000000001L);
System.out.println("aLong = " + aLong);

// 生成9个10000000000~20000000001(不含)之间的随机长整数
long[] aLong2 = RandomSource.numberSource().randomLong(10000000000L, 20000000001L, 9);
List<Long> aLong2s = LongStream.of(aLong2).mapToObj(l -> l).collect(Collectors.toList());
System.out.println("aLong2s = " + aLong2s);

// 生成1个0.01~0.51(不含)之间的随机整数
double aDouble = RandomSource.numberSource().randomDouble(0.01D, 0.51D);
System.out.println("aDouble = " + aDouble);

// 生成8个0.01~0.51(不含)之间的随机整数
double[] aDouble2 = RandomSource.numberSource().randomDouble(0.01D, 0.51D, 8);
List<Double> aDouble2s = DoubleStream.of(aDouble2).mapToObj(d -> d).collect(Collectors.toList());
System.out.println("aDouble2s = " + aDouble2s);

// 生成随机百分比
BigDecimal percent = RandomSource.numberSource().randomPercent();
System.out.println("percent = " + percent);

5.3 随机汉字

// 生成1个随机汉字
String randomChinese = RandomSource.languageSource().randomChinese();
System.out.println("randomChinese = " + randomChinese);

// 生成4个随机汉字
String randomChinese2 = RandomSource.languageSource().randomChinese(4);
System.out.println("randomChinese2 = " + randomChinese2);

5.4 随机成语

String idiom = RandomSource.languageSource().randomChineseIdiom();
System.out.println("idiom = " + idiom);

5.5 随机唐诗

Poem tangPoem = RandomSource.languageSource().randomTangPoem();
System.out.println("tangPoem = " + tangPoem);

5.6 随机人名

中文名

// 生成1个随机中文人名(性别随机)
String chineseName = RandomSource.personInfoSource().randomChineseName();
System.out.println("chineseName = " + chineseName);

// 生成1个随机男性中文人名
String maleChineseName = RandomSource.personInfoSource().randomMaleChineseName();
System.out.println("maleChineseName = " + maleChineseName);

// 生成1个随机女性中文人名
String femaleChineseName = RandomSource.personInfoSource().randomFemaleChineseName();
System.out.println("femaleChineseName = " + femaleChineseName);

英文名

// 生成1个随机英文人名
String englishName = RandomSource.personInfoSource().randomEnglishName();
System.out.println("englishName = " + englishName);

5.7 随机生成符合规则的虚拟身份证号码

// 生成1个随机的虚拟身份证号码,地区为广西壮族自治区,男性,出生日期在1990年11月11日至1999年12月12日之间
LocalDate beginDate = LocalDate.of(1990,11,11);
LocalDate endDate = LocalDate.of(1999,12,12);
String idCard = RandomSource.personInfoSource().randomMaleIdCard(Province.GX, beginDate, endDate);
System.out.println("idCard = " + idCard);

// 生成1个随机的虚拟身份证号码,地区为河北省,女性,出生日期在2001年1月11日至2008年2月22日之间
LocalDate beginDate2 = LocalDate.of(2001,1,11);
LocalDate endDate2 = LocalDate.of(2008,2,22);
String idCard2 = RandomSource.personInfoSource().randomFemaleIdCard(Province.HE, beginDate2, endDate2);
System.out.println("idCard2 = " + idCard2);

// 生成1个随机的虚拟身份证号码,地区为广西壮族自治区,男性,年龄为18岁
String idCard3 = RandomSource.personInfoSource().randomMaleIdCard(Province.GX, 18);
System.out.println("idCard3 = " + idCard3);

// 生成1个随机的虚拟身份证号码,地区为河北省,女性,年龄为19岁
String idCard4 = RandomSource.personInfoSource().randomFemaleIdCard(Province.HE, 19);
System.out.println("idCard4 = " + idCard4);

注意:

  • 身份证号码前6位地区码数据取自民政部网站2019年公开数据
  • 随机生成的虚拟身份证号码符合校验规则,但有可能与真实号码相同(纯属巧合)

5.8 随机虚拟银行卡号码

// 生成1个随机虚拟VISA信用卡号码
String cardNoOfVisa = RandomSource.personInfoSource().randomCreditCardNo(CreditCardType.Visa);
System.out.println("cardNoOfVisa = " + cardNoOfVisa);

// 生成1个随机虚拟MasterCard信用卡号码
String cardNoOfMasterCard = RandomSource.personInfoSource().randomCreditCardNo(CreditCardType.MasterCard);
System.out.println("cardNoOfMasterCard = " + cardNoOfMasterCard);

// 生成1个随机虚拟American Express信用卡号码
String cardNoOfAmex = RandomSource.personInfoSource().randomCreditCardNo(CreditCardType.Amex);
System.out.println("cardNoOfAmex = " + cardNoOfAmex);

// 生成1个随机虚拟银联信用卡号码
String cardNoOfUnionPay = RandomSource.personInfoSource().randomCreditCardNo(CreditCardType.UnionPay);
System.out.println("cardNoOfUnionPay = " + cardNoOfUnionPay);

// 生成1个随机虚拟JCB信用卡号码
String cardNoOfJCB = RandomSource.personInfoSource().randomCreditCardNo(CreditCardType.JCB);
System.out.println("cardNoOfJCB = " + cardNoOfJCB);

注意:

  • 随机生成的虚拟银行卡号码只是前缀和位数符合规则,不会与现实中的真实卡号产生重合,无法用于支付,仅供模拟测试/仿真/项目演示等用途

5.9 随机中国大陆手机号

// 生成1个随机中国大陆手机号
String chineseMobile = RandomSource.personInfoSource().randomChineseMobile();
System.out.println("chineseMobile = " + chineseMobile);

5.10 随机邮箱地址

// 生成1个随机邮箱地址,后缀随机,邮箱用户名最大长度为10
String randomEmail = RandomSource.internetSource().randomEmail(10);
System.out.println("randomEmail = " + randomEmail);

// 生成1个随机邮箱地址,后缀为163.com,邮箱用户名最大长度为10
String randomEmail2 = RandomSource.internetSource().randomEmail(10, "163.com");
System.out.println("randomEmail2 = " + randomEmail2);

5.11 随机域名

// 生成1个随机域名,域名最大长度为16
String randomDomain = RandomSource.internetSource().randomDomain(16);
System.out.println("randomDomain = " + randomDomain);

5.12 随机ipv4地址

// 生成1个随机公网IPv4地址
String publicIpv4 = RandomSource.internetSource().randomPublicIpv4();
System.out.println("publicIpv4 = " + publicIpv4);

// 生成1个随机私有(内网)IPv4地址
String privateIpv4 = RandomSource.internetSource().randomPrivateIpv4();
System.out.println("privateIpv4 = " + privateIpv4);

5.13 随机ipv6地址

// 生成1个随机ipv6地址
String randomIpV6 = RandomSource.internetSource().randomIpV6();
System.out.println("randomIpV6 = " + randomIpV6);

5.14 随机端口号

// 生成1个随机端口号
int randomPort = RandomSource.internetSource().randomPort();
System.out.println("randomPort = " + randomPort);

5.15 随机app信息

// 生成1个随机App名称
String randomAppName = RandomSource.internetSource().randomAppName();
System.out.println("randomAppName = " + randomAppName);

// 生成1个随机App Bundle ID
String randomAppBundleId = RandomSource.internetSource().randomAppBundleId();
System.out.println("randomAppBundleId = " + randomAppBundleId);

// 生成1个随机App版本号
String randomAppVersionCode = RandomSource.internetSource().randomAppVersionCode();
System.out.println("randomAppVersionCode = " + randomAppVersionCode);

5.16 随机静态url

// 生成1个随机静态url,后缀为jpg
String randomStaticUrl = RandomSource.internetSource().randomStaticUrl("jpg");
System.out.println("randomStaticUrl = " + randomStaticUrl);

5.17 随机日期

// 生成1个2020年的随机日期,日期格式为yyyy-MM-dd
String randomDate = RandomSource.dateTimeSource().randomDate(2020, "yyyy-MM-dd");
System.out.println("randomDate = " + randomDate);

// 生成1个2020年1月2日之后的随机日期,日期格式为yyyy-MM-dd
String randomFutureDate = RandomSource.dateTimeSource().randomFutureDate(LocalDate.of(2020, 1, 2), "yyyy-MM-dd");
System.out.println("randomFutureDate = " + randomFutureDate);

// 生成1个今天(基于系统时间判断)之后的随机日期,日期格式为yyyy-MM-dd
String randomFutureDate2 = RandomSource.dateTimeSource().randomFutureDate("yyyy-MM-dd");
System.out.println("randomFutureDate2 = " + randomFutureDate2);

// 生成1个2020年1月2日之前1年内的随机日期,日期格式为yyyy-MM-dd
String randomPastDate = RandomSource.dateTimeSource().randomPastDate(LocalDate.of(2020, 1, 2), "yyyy-MM-dd");
System.out.println("randomPastDate = " + randomPastDate);

// 生成1个今天(基于系统时间判断)之前1年内的随机日期,日期格式为yyyy-MM-dd
String randomPastDate2 = RandomSource.dateTimeSource().randomPastDate("yyyy-MM-dd");
System.out.println("randomPastDate2 = " + randomPastDate2);

// 生成1个2020年1月2日之前10年内的随机日期,日期格式为yyyy-MM-dd
String randomPastDate3 = RandomSource.dateTimeSource().randomPastDate(LocalDate.of(2020, 1, 2), 3650, "yyyy-MM-dd");
System.out.println("randomPastDate3 = " + randomPastDate3);

// 生成1个2000年1月11日至2010年2月22日范围之间的随机日期,日期格式为yyyy-MM-dd
LocalDate beginDate = LocalDate.of(2000, 1, 11);
LocalDate endDate = LocalDate.of(2010, 2, 22);
String randomDate2 = RandomSource.dateTimeSource().randomDate(beginDate, endDate, "yyyy-MM-dd");
System.out.println("randomDate2 = " + randomDate2);

5.18 随机时间

// 生成过去7天范围内的随机时间
LocalDateTimerandomPastTime = RandomSource.dateTimeSource().randomPastTime(7);
System.out.println("randomPastTime = " + randomPastTime);

// 生成未来7天范围内的随机时间
LocalDateTime randomFutureTime = RandomSource.dateTimeSource().randomFutureTime(7);
System.out.println("randomFutureTime = " + randomFutureTime);

// 生成2020年2月14日当天范围内的随机时间
LocalDateTime randomTime = RandomSource.dateTimeSource().randomTime(2020, 2, 14);
System.out.println("randomTime = " + randomTime);

// 生成过去100秒范围内的随机时间
LocalDateTime randomPastTime2 = RandomSource.dateTimeSource().randomPastTime(LocalDateTime.now(), 100);
System.out.println("randomPastTime2 = " + randomPastTime2);

// 生成未来100秒范围内的随机时间
LocalDateTime randomFutureTime2 = RandomSource.dateTimeSource().randomFutureTime(LocalDateTime.now(), 100);
System.out.println("randomFutureTime2 = " + randomFutureTime2);

5.19 随机时间戳

// 生成1个当天范围内的随机时间戳
long randomTimestamp = RandomSource.dateTimeSource().randomTimestamp(LocalDate.now());
System.out.println("randomTimestamp = " + randomTimestamp);

// 生成1个2020年3月6日范围内的随机时间戳
long randomTimestamp2 = RandomSource.dateTimeSource().randomTimestamp(LocalDate.of(2020, 3, 6));
System.out.println("randomTimestamp2 = " + randomTimestamp2);

// 生成1个介于2020年3月6日12:00:00至2020年3月6日12:30:00之间的随机时间戳
LocalDateTime begin = LocalDateTime.of(2020, 3, 6, 12, 0, 0);
LocalDateTime end = LocalDateTime.of(2020, 3, 6, 12, 30, 0);
long randomTimestamp3 = RandomSource.dateTimeSource().randomTimestamp(begin, end);
System.out.println("randomTimestamp3 = " + randomTimestamp3);

// 生成1个2020年3月6日12:00:00之后180秒内的随机时间戳
LocalDateTime base1 = LocalDateTime.of(2020, 3, 6, 12, 0, 0);
long randomFutureTimestamp = RandomSource.dateTimeSource().randomFutureTimestamp(base1, 180);
System.out.println("randomFutureTimestamp = " + randomFutureTimestamp);

// 生成1个2020年3月7日13:00:00之前120秒内的随机时间戳
LocalDateTime base2 = LocalDateTime.of(2020, 3, 7, 13, 0, 0);
long randomPastTimestamp = RandomSource.dateTimeSource().randomPastTimestamp(base2, 120);
System.out.println("randomPastTimestamp = " + randomPastTimestamp);

5.20 随机时区名称

// 生成1个随机时区名称
String randomTimezoneName = RandomSource.dateTimeSource().randomTimezoneName();
System.out.println("randomTimezoneName = " + randomTimezoneName);

5.21 随机强密码

// 生成1个随机强密码,长度为16,无特殊字符
String randomStrongPassword = RandomSource.personInfoSource().randomStrongPassword(16, false);
System.out.println("randomStrongPassword = " + randomStrongPassword);

// 生成1个随机强密码,长度为16,有特殊字符
String randomStrongPassword2 = RandomSource.personInfoSource().randomStrongPassword(16, true);
System.out.println("randomStrongPassword2 = " + randomStrongPassword2);

5.22 随机地址

// 随机获取省份
String randomProvince = RandomSource.areaSource().randomProvince();
System.out.println("randomProvince = " + randomProvince);

// 随机获取城市(省份+城市,以逗号为分隔符)
String randomCity = RandomSource.areaSource().randomCity(",");
System.out.println("randomCity = " + randomCity);

// 随机获取邮编
String randomZipCode = RandomSource.areaSource().randomZipCode();
System.out.println("randomZipCode = " + randomZipCode);

// 生成1个随机中国大陆详细地址
String randomAddress = RandomSource.areaSource().randomAddress();
System.out.println("randomAddress = " + randomAddress);

5.23 随机国家或地区

// 随机获取1个编码首字母为b的国家或地区
CountryOrRegionCode countryOrRegionCode = RandomSource.areaSource().randomCountryOrRegionCode("b");
System.out.println("countryOrRegionCode = " + countryOrRegionCode);

// 随机获取1个国家或地区(不限首字母)
CountryOrRegionCode countryOrRegionCode2 = RandomSource.areaSource().randomCountryOrRegionCode();
System.out.println("countryOrRegionCode2 = " + countryOrRegionCode2);

5.24 随机经纬度

// 随机生成1个纬度
double randomLatitude = RandomSource.areaSource().randomLatitude();
System.out.println("randomLatitude = " + randomLatitude);

// 随机生成1个经度
double randomLongitude = RandomSource.areaSource().randomLongitude();
System.out.println("randomLongitude = " + randomLongitude);

5.25 随机固话

// 随机固话区号(省级行政区名称不需要包含后缀)
String randomPhoneCode = RandomSource.areaSource().randomPhoneCode("湖南");
System.out.println("randomPhoneCode = " + randomPhoneCode);
// 随机固话号码(使用-作为分隔符,默认的分隔符是空格)
String randomPhoneNumber = RandomSource.areaSource().randomPhoneNumber("广东", "-");
System.out.println("randomPhoneNumber = " + randomPhoneNumber);

5.26 随机中国大陆车牌号

// 生成1个随机中国大陆车牌号(新能源车型)
String randomPlateNumber = RandomSource.otherSource().randomPlateNumber(true);
System.out.println("randomPlateNumber = " + randomPlateNumber);

// 生成1个随机中国大陆车牌号(非新能源车型)
String randomPlateNumber2 = RandomSource.otherSource().randomPlateNumber();
System.out.println("randomPlateNumber2 = " + randomPlateNumber2);

5.27 随机网络昵称

// 生成1个随机英文网络昵称,最大长度为8个字符
String randomNickName = RandomSource.personInfoSource().randomNickName(8);
System.out.println("randomNickName = " + randomNickName);

// 生成1个随机汉字网络昵称,最大长度为8个汉字
String randomChineseNickName = RandomSource.personInfoSource().randomChineseNickName(8);
System.out.println("randomChineseNickName = " + randomChineseNickName);

// 基于随机汉字网络昵称生成1个拼音网络昵称,最大长度为4个汉字
String randomPinyinNickName = RandomSource.personInfoSource().randomPinyinNickName(4);
System.out.println("randomPinyinNickName = " + randomPinyinNickName);

5.28 随机qq信息

// 生成1个随机QQ号
String randomQQAccount = RandomSource.personInfoSource().randomQQAccount();
System.out.println("randomQQAccount = " + randomQQAccount);

// 生成1个随机非主流QQ网名
String randomQQNickName = RandomSource.personInfoSource().randomQQNickName();
System.out.println("randomQQNickName = " + randomQQNickName);

5.29 随机民族名称

String randomEthnicName = RandomSource.otherSource().randomEthnicName();
System.out.println("randomEthnicName = " + randomEthnicName);

5.30 随机教育背景信息

// 随机获取学历
String randomDegree = RandomSource.educationSource().randomDegree();
System.out.println("randomDegree = " + randomDegree);

// 随机获取本科高校名称
String randomCollege = RandomSource.educationSource().randomCollege();
System.out.println("randomCollege = " + randomCollege);

// 随机高校专业名称
String randomMajorName = RandomSource.educationSource().randomMajorName();
System.out.println("randomMajorName = " + randomMajorName);

// 随机获取小学名称
String randomPrimarySchoolName = RandomSource.educationSource().randomPrimarySchoolName();
System.out.println("randomPrimarySchoolName = " + randomPrimarySchoolName);

// 随机获取小学年级
String randomPrimarySchoolGrade = RandomSource.educationSource().randomPrimarySchoolGrade();
System.out.println("randomPrimarySchoolGrade = " + randomPrimarySchoolGrade);

// 随机获取中学名称
String randomHighSchoolName = RandomSource.educationSource().randomHighSchoolName();
System.out.println("randomHighSchoolName = " + randomHighSchoolName);

// 随机获取中学年级
String randomHighSchoolGrade = RandomSource.educationSource().randomHighSchoolGrade();
System.out.println("randomHighSchoolGrade = " + randomHighSchoolGrade);

// 随机班级名称
String randomClassName = RandomSource.educationSource().randomClassName();
System.out.println("randomClassName = " + randomClassName);

5.31 随机公司及部门名称

// 随机生成1个公司名称,地区前缀为北京
String companyName = RandomSource.otherSource().randomCompanyName("北京");
System.out.println("companyName = " + companyName);
// 随机生成1个公司部门名称
String companyDepartment = RandomSource.otherSource().randomCompanyDepartment();
System.out.println("companyDepartment = " + companyDepartment);

5.32 随机中文短句

// 随机生成1条中文短句
StringchineseSentence = RandomSource.languageSource().randomChineseSentence();
System.out.println("chineseSentence = " + chineseSentence);

5.33 随机英文文本

// 随机生成1条英文文本,包含10个单词
String randomEnglishText = RandomSource.languageSource().randomEnglishText(10);
System.out.println("randomEnglishText = " + randomEnglishText);

5.34 随机营销号

String randomNonsenseTitle = RandomSource.languageSource().randomNonsenseTitle("星期一", "下雨");
System.out.println("randomNonsenseTitle = " + randomNonsenseTitle);
String randomNonsense = RandomSource.languageSource().randomNonsense("星期一", "下雨");
System.out.println("randomNonsense = " + randomNonsense);

5.35 随机行业分类

EconomicCategory economicCategory = RandomSource.otherSource().randomEconomicCategory();
System.out.println("economicCategory = " + economicCategory);

5.36 统一社会信用代码

String socialCreditCode = RandomSource.otherSource().randomSocialCreditCode();
System.out.println("socialCreditCode = " + socialCreditCode);

5.37 随机EAN

// 随机ISBN,返回结果需要分隔符-,格式例如:978-7-XXXX-XXXX-X
String randomISBN = RandomSource.otherSource().randomISBN(true);
System.out.println("randomISBN = " + randomISBN);

// 随机ISBN,返回结果不需要分隔符,格式例如:9787XXXXXXXXX
String randomISBN1 = RandomSource.otherSource().randomISBN(false);
System.out.println("randomISBN1 = " + randomISBN1);

// 随机国际商品编码,格式例如:691XXXXXXXXXX
String randomEAN = RandomSource.otherSource().randomEAN();
System.out.println("randomEAN = " + randomEAN);

说明:

  • 本程序随机生成的EAN编码长度为13位
  • 本程序随机生成的EAN编码符合标准校验规则,仅供模拟测试/仿真/项目演示等用途,并不表示现实中真实存在该编码对应的商品(如有雷同纯属巧合)

5.38 随机useragent

// 随机生成1个PC User-Agent
String randomPCUserAgent = RandomSource.internetSource().randomPCUserAgent();
System.out.println("randomPCUserAgent = " + randomPCUserAgent);

// 随机生成1个Android User-Agent
String randomAndroidUserAgent = RandomSource.internetSource().randomAndroidUserAgent();
System.out.println("randomAndroidUserAgent = " + randomAndroidUserAgent);

// 随机生成1个iOS User-Agent
String randomIOSUserAgent = RandomSource.internetSource().randomIOSUserAgent();
System.out.println("randomIOSUserAgent = " + randomIOSUserAgent);

5.39 随机网卡mac地址

// 随机生成1个网卡MAC地址,使用:作为分隔符
StringmacAddress = RandomSource.internetSource().randomMacAddress(":");
System.out.println("macAddress = " + macAddress);

5.40 随机颜色值

// 随机生成1个RGB颜色值
int[] randomRgbColor = RandomSource.otherSource().randomRgbColor();
List<Integer> collect = IntStream.of(randomRgbColor).mapToObj(i -> i).collect(Collectors.toList());
System.out.println("collect = " + collect);

// 随机生成1个16进制(HEX)颜色值
String randomHexColor = RandomSource.otherSource().randomHexColor();
System.out.println("randomHexColor = " + randomHexColor);

5.41 股票名称和股票代码

// 随机股票信息(沪A+深A+创业板+科创版)
String[] stock = RandomSource.financialSource().randomStock();
String stockName = stock[0];
String stockCode = stock[1];
System.out.println("stockName = " + stockName + " -> stockCode = " + stockCode);

// 随机股票信息(港股)
String[] hkStock = RandomSource.financialSource().randomHKStock();
String hkStockName = hkStock[0];
String hkStockCode = hkStock[1];
System.out.println("hkStockName = " + hkStockName + " -> hkStockCode = " + hkStockCode);

// 随机股票信息(新三板)
String[] xsbStock = RandomSource.financialSource().randomXsbStock();
String xsbStockName = xsbStock[0];
String xsbStockCode = xsbStock[1];
System.out.println("xsbStockName = " + xsbStockName + " -> xsbStockCode = " + xsbStockCode);

// 随机股票信息(北交所)
String[] bseStock = RandomSource.financialSource().randomBseStock();
String bseStockName = bseStock[0];
String bseStockCode = bseStock[1];
System.out.println("bseStockName = " + bseStockName + " -> bseStockCode = " + bseStockCode);

// 随机股票代码(纳斯达克)
String nasdaqStock = RandomSource.financialSource().randomNasdaqStock();
System.out.println("nasdaqStock = " + nasdaqStock);

5.42 开放式基金名称和基金代码

String[] fund = RandomSource.financialSource().randomFund();
String fundName = fund[0];
System.out.println("fundName = " + fundName);
String fundCode = fund[1];
System.out.println("fundCode = " + fundCode);

5.43 日k线数据

// 随机生成20210201~20210228日期范围内的K线数据,起始价格为100,单日最大涨幅10%,单日最大跌幅-10%
List<KChartData> kList = RandomSource.financialSource().randomDailyKChartData(100, 0.1, -0.1, "20210201", "20210228");

5.44 货币

// 随机获取一种货币信息
CurrencyInfo currencyInfo = RandomSource.financialSource().randomCurrencyInfo();
System.out.println("currencyInfo = " + currencyInfo);

5.45 足球联赛球队名称

// 英超
String footballTeam = RandomSource.sportSource().randomFootballTeam(CompetitionType.PREMIER_LEAGUE);
System.out.println("footballTeam = " + footballTeam);

// 西甲
String footballTeam1 = RandomSource.sportSource().randomFootballTeam(CompetitionType.LA_LIGA);
System.out.println("footballTeam1 = " + footballTeam1);

// 德甲
String footballTeam2 = RandomSource.sportSource().randomFootballTeam(CompetitionType.BUNDESLIGA);
System.out.println("footballTeam2 = " + footballTeam2);

// 意甲
String footballTeam3 = RandomSource.sportSource().randomFootballTeam(CompetitionType.SERIE_A);
System.out.println("footballTeam3 = " + footballTeam3);

// 法甲
String footballTeam4 = RandomSource.sportSource().randomFootballTeam(CompetitionType.LIGUE_1);
System.out.println("footballTeam4 = " + footballTeam4);

// 荷甲
String footballTeam5 = RandomSource.sportSource().randomFootballTeam(CompetitionType.EREDIVISIE);
System.out.println("footballTeam5 = " + footballTeam5);

5.46 篮球联赛球队名称

// CBA
String basketballTeam = RandomSource.sportSource().randomBasketballTeam(CompetitionType.CBA);
System.out.println("basketballTeam = " + basketballTeam);

// NBA
String basketballTeam2 = RandomSource.sportSource().randomBasketballTeam(CompetitionType.NBA);
System.out.println("basketballTeam2 = " + basketballTeam2);

5.47 国家及地区的足球代表队

// (亚洲足联范围内)随机足球队名称
FootballTeam randomFootballTeam = RandomSource.sportSource().randomFootballTeam(FootballConfederation.AFC);
System.out.println("randomFootballTeam = " + randomFootballTeam);

// (欧洲足联范围内)随机足球队名称
FootballTeam randomFootballTeam2 = RandomSource.sportSource().randomFootballTeam(FootballConfederation.UEFA);
System.out.println("randomFootballTeam2 = " + randomFootballTeam2);

// 随机足球队名称(不限足球联合会)
FootballTeam randomFootballTeam3 = RandomSource.sportSource().randomFootballTeam();
System.out.println("randomFootballTeam3 = " + randomFootballTeam3);

5.48 热门手机型号

String randomMobileModel = RandomSource.otherSource().randomMobileModel();
System.out.println("randomMobileModel = " + randomMobileModel);

六、数据生成工具

此工具类支持自定义生成符合业务需求的随机数据,示例如下:

//准备字段定义
//字段name:随机姓名
DataField df1 = new DataField("name", () -> RandomSource.personInfoSource().randomChineseName());
//字段birthDate:随机日期
DataField df2 = new DataField("birthDate", () -> RandomSource.dateTimeSource().randomPastDate("yyyy-MM-dd"));
//字段salary:随机数字
DataField df3 = new DataField("salary", () -> RandomSource.numberSource().randomInt(5000, 18000));
List<DataField> fieldList = Lists.newArrayList(df1, df2, df3);

//设置数量
int total = 10;

//生成JSON
String json = DataUtils.generateJson(fieldList, total); 

//生成CSV 
String csv = DataUtils.generateCsv(fieldList, total); 

//生成SQL之前先要指定表名
String tableName = "user";
//生成SQL插入语句
String sql = DataUtils.generateJson(fieldList, tableName, total);     

七、更多设置

详见:Github:Java随机数据生成器

你可能感兴趣的:(Java,java)