Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

mysql - random numbers in java

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

mysql> select * from consumer9;
-------------             
4 rows in set (0.13 sec)



Service_ID          Service_Type                            consumer_feedback 

100                    computing                                          -1
35                     printer                                             0
73                    computing                                           -1
50                     data                                                1

I have generated these values using the concept of random numbers. I want to get the output where the Service_types(Printer,Computing,data) are distributed uniformally in all the tables with the feedback values of 1 occuring most number of times.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...