S.PRATHIBA
S.PRATHIBA

Reputation: 19

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.

Upvotes: 1

Views: 1182

Answers (1)

trashgod
trashgod

Reputation: 205785

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.

Upvotes: 5

Related Questions