random numbers in java

前端 未结 1 1531
再見小時候
再見小時候 2020-12-12 03:15

I have the following table created using java as the front end and mysql as the backend.

mysql> select * from consumer9;
-------------             
4 rows         


        
相关标签:
1条回答
  • 2020-12-12 03:24

    The class java.util.Random can generate pseudo-random numbers having a reasonably uniform distribution. Given a List of your service type:

    List<String> services = new ArrayList<String>(
        Arrays.asList("COMPUTER", "DATA", "PRINTER"));
    

    it is easy to select one at random:

    String s = services.get(rnd.nextInt(services.size()));
    

    Similarly, one of a list of feedback values may be chosen:

    List<String> feedbacks = new ArrayList<String>(
        Arrays.asList("1", "0", "-1"));
    String s = feedbacks.get(rnd.nextInt(feedbacks.size()));
    

    One simple expedient to get a different distribution is to "stack the deck". For example,

    Arrays.asList("1", "1", "1", "0", "0", "-1"));
    

    would produce 1, 0, and -1 with probability 1/2, 1/3, and 1/6, respectively. You can arrange more elaborate partitions using nextGaussian() and a suitable confidence interval.

    This approach should only be used for generating test data.

    Addendum: The Apache Commons Math Guide includes a chapter on Data Generation, with informative links and documentation concerning other probability distributions.

    0 讨论(0)
提交回复
热议问题