Java中的随机数 [英] random numbers in java

查看:75
本文介绍了Java中的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Java作为前端和mysql作为后端创建的下表.

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

我已经使用随机数的概念生成了这些值. 我想获得其中Service_types(Printer,Computing,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.

推荐答案

java.util.Random 可以生成具有合理均匀分布的伪随机数.给定您的服务类型List:

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

很容易随机选择一个:

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"));

会以概率 1 / 2 1 / 3 产生1,0和-1,和 1 / 6 .您可以使用 nextGaussian() 和合适的置信区间.

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.

附录: Apache Commons Math Guide 包括有关 数据生成 的一章,与其他概率分布有关的信息链接和文档.

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

这篇关于Java中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆