代码随机生成想要的数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/meiceatcsdn/article/details/81263681

各位读者,下午好啊,寡人小美。这里简单介绍一款工具类,自己随机生成一些想要的数据,话不多说,直接上菜。

目录


1、自己造数据
2、总结


1、自己造数据

这里面用到了枚举,如果没记错的话,基础课程枚举并不是重点,甚至会一掠而过。实际工作中,枚举多用来表示状态机。

以下是:DataSimular.java

package com.hmc.auth;

import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.xml.crypto.Data;

import org.apache.catalina.startup.Tomcat.ExistingStandardWrapper;
import org.apache.tomcat.jni.SSLContext.SNICallBack;

/**
Author:meice Huang
Time:2018年7月24日下午11:58:28
*/
public class DataSimulator {
    public static int randomPositiveInt(int max) {
        if(max<0) {
            throw new  IllegalArgumentException("upper boundary can not less than zero");
        }
        return randomInt(0, max);
    }

    public static int randomInt(int min,int max) {
        Random random = new Random();
        return random.nextInt(max - min)+min;
    }
    /**
     * nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    官方系统体统了一个参数的方法,为什么不是2个参数呢?因为没必要。
    eg:random.nextInt(5)会生成[0,5)的整数,如果我想生成[3,5)之间的整数,怎么办?需要定义2个参数,一个min,一个max么?NO!
    我们只需要这样操作即可:random.nextInt(max-min)+min,这样会先在[0,(max-min))之间产生2个随机整数,但是我们需要的是[min,max)之间的?那就加上min即可。
     */

    public static String randomCharacters(int limit) {
        if(limit<0) {
            throw new IllegalArgumentException("limit can not less than zero");
        }
        char[] ss = new char[limit];
        int i = 0;
        while(i<limit) {
            int f = (int)(Math.random()*3%3);//这里为什么这么取值?
            if(f==0)
                    ss[i] = (char)('A'+randomPositiveInt(26));
            else if(f==1)
                    ss[i] = (char)('a'+randomPositiveInt(26));
            else
                    ss[i] = (char)('0'+randomPositiveInt(10));
            i++;
        }
        return new String(ss);
    }

    /**
     * 以上这个场景还是蛮多的,比如验证码或者其他的东东
     */


    public static String randomAgeUnit() {
        int mod = randomPositiveInt(3)%3;
        if(mod == 0) {
            return "岁";
        } else if(mod == 1) {
            return "月";
        } else 
            return "天";
    }

    public static String randomGender() {
        int mod = randomPositiveInt(2)%2;
        if(mod == 0) {
            return "MALE";
        }else 
            return "FEMALE";
    }


    public static MedicationType randomMedicationType() {
        int mod = randomPositiveInt(3)%3;
        if(mod == 0)
            return MedicationType.INJECTION;
        else if(mod == 1)
            return MedicationType.ORAL;
        else 
            return MedicationType.TOPICAL;
    }


    public static DrugFrequencyUnit randomDrugRrequency() {
        int mod = randomPositiveInt(2) % 2;
        if(mod == 0)
            return DrugFrequencyUnit.MULTIPLE_DAY_ONE_TIME;
        else 
            return DrugFrequencyUnit.ONE_DAY_MULTIPLE_TIMES;
    }


    public static TreatmentStatus randomTreatmentStatus() {
        int mod = randomPositiveInt(4) % 4;
        if(mod == 0) 
            return  TreatmentStatus.FINISHED_EFFECTIVE;
        else if(mod == 1)
            return TreatmentStatus.FINISHED_INEFFECTIVE;
        else if(mod == 2)
            return TreatmentStatus.FINISHED_UNKNOWN;
        else 
            return TreatmentStatus.UNFINISHED;

    }

    public static String randomDrugUnit() {
        int mod = randomPositiveInt(8) % 8;
        switch (mod) {
            case 0:
                return  "g/次";
            case 1:
                return "mg/次";
            case 2:
                return "ug/次";
            case 3:
                return "ng/次";
            case 4:
                return "L/次";
            case 5:
                return "ml/次";
            case 6:
                return "IU/次";
            case 7:
                return "U/次";
            default:
                return "g/次";
        }
    }

    public static int randomDistrict() {
        return 110101;
    }

    public static String[] randomDiseases() {
        return new String[] {"干眼综合症","兔眼"};
    }

    public static Long randomLong() {
        long min = 1;
        long max = 10;
        return min + ((long)(new Random().nextDouble()*(max - min)));
    }


    public static long randomDate(long begin,long end) {
        long rtn = begin + (long)Math.random()*(begin -end);
        if(rtn == begin|| rtn == end)
             return randomDate(begin, end);//Exception in thread "main" java.lang.StackOverflowError
        return rtn;
    }


    public static String randomPhoneNum(String begin) {//这里为什么要带一个参数呢?
        int l = begin.length();
        StringBuilder sb = new StringBuilder(20);
        sb.append(begin);
        Random random = new Random();
        for(int i=0;i<11-l;i++) {
            sb.append(random.nextInt(10));//[0,10)
        }
        return sb.toString();

    }


    public static String getRandomPhoneNum() {
        StringBuffer sb = new StringBuffer();
        Random random  = new Random();
        for(int i=0;i<11;i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    public static String randomIDCard() throws ParseException {//不怎么懂最后一位为什么要这么生成?
        StringBuilder sb = new StringBuilder(20);
        Random random = new Random();
        sb.append(random.nextInt(9)+1);//[1,10)
        //6 10221 19840407 0922
        for(int i=0;i<5;i++) {
            sb.append(random.nextInt(10));//[0,10)
        }
        sb.append(new SimpleDateFormat("yyMMdd").format(randomDate("19000101", "20180101")));

        for(int i=0;i<3;i++) {
            sb.append(random.nextInt(10));
        }
        sb.append(lastNumber(sb.toString()));
        return sb.toString();
    }


    public static char lastNumber(String ID17) {//???What ?
        int x[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        char y[] = {'1','0','x','9','8','7','6','5','4','3','2'};
        int sum = 0;
        for(int i=0;i<17;i++) {
            int n = ID17.charAt(i)-48;
            sum = sum+n*x[i];
        }
        sum = sum%11;
        return y[sum];

    }


    public static boolean randomTrue() {
        int mod = randomPositiveInt(2)%2;
        return mod == 0;
    }

    public static BigDecimal randomPoint(int max) {
        if(max <0)
            throw new IllegalArgumentException("Point should not be less than zero");
        else {
            Double doublePoint = Math.random()*max;
            return new BigDecimal(doublePoint);
        }
    }


    public static MessageTypeEnum randomMessageType() {
        int mod = randomPositiveInt(8) % 8;
        switch(mod) {
        case 0:
                return MessageTypeEnum.MSL;
        case 1:
                return MessageTypeEnum.FANS;
        case 2:
                return MessageTypeEnum.CASE;
        case 3:
                return MessageTypeEnum.POINT;
        case 4:
                return MessageTypeEnum.NOTICE;
        case 5:
            return MessageTypeEnum.ISSUES;
        case 6:
            return MessageTypeEnum.RELATION;
        default :
            return MessageTypeEnum.USER_CARD;
        }
    }


    public static Date randomDate(String beginDate,String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        Date start = sdf.parse(beginDate);
        Date end  = sdf.parse(endDate);
        if(start.getTime()>=end.getTime()) 
            return null;
        long date = randomDate(start.getTime(), end.getTime());
        return new Date(date);
    }


    public static void main(String[] args) {
        System.out.println(DataSimulator.randomPositiveInt(7)+3);
        System.out.println(DataSimulator.randomCharacters(5));
        System.out.println(Math.random()*3%3);
        System.out.println((int)(Math.random()*3%3));

        System.out.println(randomPositiveInt(3)%3);
        //DataSimulator.randomDate(120, 10);
        System.out.println("=========");
        System.out.println(DataSimulator.randomPhoneNum("kdalfdkalf"));
        System.out.println(DataSimulator.getRandomPhoneNum());

    }
}

再放几个枚举类做例子,枚举类会专门发表博客做总结。

eg1:MedicationType.enum

package com.hmc.auth;
/**
Author:meice Huang
Time:2018年7月28日上午5:10:06
*/
public enum MedicationType {
    ORAL,//口服
    INJECTION,//注射
    TOPICAL;//外敷
}

eg2:DrugFrequencyUnit.enum

package com.hmc.auth;
/**
Author:meice Huang
Time:2018年7月28日上午5:16:16
*/
public enum DrugFrequencyUnit {
    //用药按天算,每天几次
    ONE_DAY_MULTIPLE_TIMES,
    //用药按次算,每几天一次
    MULTIPLE_DAY_ONE_TIME;
}

2、总结


1)以上方法有不少都是重复的。不过,上面给出的一个道理就是,如果你需要什么数据,OK,自己造。
2)产生随机数的方法randomPositiveInt(n)%m,这里可以知道,需要几种类型,就可以除以几,Why?数学知识。
3)注意枚举类的活学活用。

欢迎评论交流,下期再会!

猜你喜欢

转载自blog.csdn.net/meiceatcsdn/article/details/81263681