Java 通过生成随机数,根据这些百分比随机选择表格
我正在从事一个项目,在该项目中,我在不同的数据库中有两个具有不同模式的表。这意味着我有两个不同的连接参数用于使用JDBC连接这两个表- 假设下面是config.property文件,其中Java 通过生成随机数,根据这些百分比随机选择表格,java,random,Java,Random,我正在从事一个项目,在该项目中,我在不同的数据库中有两个具有不同模式的表。这意味着我有两个不同的连接参数用于使用JDBC连接这两个表- 假设下面是config.property文件,其中table1。百分比表示将拾取80%时间table1,并基于随机数拾取20%时间table2 TABLES: table1 table2 #For Table1 table1.percentage: 80 #For Table2 table2.percentage: 20 下面的方法将读取上面的co
table1。百分比
表示将拾取80%
时间table1
,并基于随机数拾取20%
时间table2
TABLES: table1 table2
#For Table1
table1.percentage: 80
#For Table2
table2.percentage: 20
下面的方法将读取上面的config.property
文件,并为每个表创建一个ReadTableConnectionInfo
对象
private static HashMap<String, ReadTableConnectionInfo> tableList = new HashMap<String, ReadTableConnectionInfo>();
private static void readPropertyFile() throws IOException {
prop.load(Read.class.getClassLoader().getResourceAsStream("config.properties"));
tableNames = Arrays.asList(prop.getProperty("TABLES").split(" "));
for (String arg : tableNames) {
ReadTableConnectionInfo ci = new ReadTableConnectionInfo();
double percentage = Double.parseDouble(prop.getProperty(arg + ".percentage"));
ci.setPercentage(percentage);
tableList.put(arg, ci);
}
}
private static Random random = new SecureRandom();
@Override
public run() {
...
while ( < 60 minutes) {
double randomNumber = random.nextDouble() * 100.0;
ReadTableConnectionInfo tableInfo = selectRandomConnection(randomNumber);
// do query...
}
}
//Any problem with the below method?
private ReadTableConnectionInfo selectRandomConnection(double randomNumber) {
double limit = 0;
for (ReadTableConnectionInfo ci : tableLists.values()) {
limit += ci.getPercentage();
if (randomNumber < limit) {
return ci;
}
}
throw new IllegalStateException();
}
现在,在我的run方法中,每个线程必须生成随机数,然后根据每个表的百分比决定需要使用哪个表
private static HashMap<String, ReadTableConnectionInfo> tableList = new HashMap<String, ReadTableConnectionInfo>();
private static void readPropertyFile() throws IOException {
prop.load(Read.class.getClassLoader().getResourceAsStream("config.properties"));
tableNames = Arrays.asList(prop.getProperty("TABLES").split(" "));
for (String arg : tableNames) {
ReadTableConnectionInfo ci = new ReadTableConnectionInfo();
double percentage = Double.parseDouble(prop.getProperty(arg + ".percentage"));
ci.setPercentage(percentage);
tableList.put(arg, ci);
}
}
private static Random random = new SecureRandom();
@Override
public run() {
...
while ( < 60 minutes) {
double randomNumber = random.nextDouble() * 100.0;
ReadTableConnectionInfo tableInfo = selectRandomConnection(randomNumber);
// do query...
}
}
//Any problem with the below method?
private ReadTableConnectionInfo selectRandomConnection(double randomNumber) {
double limit = 0;
for (ReadTableConnectionInfo ci : tableLists.values()) {
limit += ci.getPercentage();
if (randomNumber < limit) {
return ci;
}
}
throw new IllegalStateException();
}
private static Random=new SecureRandom();
@凌驾
公营{
...
而(<60分钟){
双随机数=random.nextDouble()*100.0;
ReadTableConnectionInfo tableInfo=选择随机连接(随机数);
//做查询。。。
}
}
//下面的方法有什么问题吗?
专用ReadTableConnectionInfo selectRandomConnection(双随机数){
双限=0;
对于(ReadTableConnectionInfo ci:TableList.values()){
limit+=ci.getPercentage();
if(随机数<限制){
返回ci;
}
}
抛出新的非法状态异常();
}
问题陈述:-
我的selectRandomConnection
方法有问题吗?因为每个线程工作60分钟,并且在这60分钟内,它每次只选择表1
我要找的是
80%
它应该选择table 1
和20%
它应该选择table 2
如果你想让A在80%的时间里被选择,而B在20%的时间里被选择,平均来说,只需选择一个介于0(包括)和100(不包括)之间的随机数。如果数字小于80,选择A,否则选择B
就这么简单。它正在选择表这一事实与此无关。您要做的是从一组元素中选择一个随机元素,但任何元素被选中的概率不是集合大小的1,而是其他一些数字。现在,请解释为什么您认为
selectRandomConnection
应该这样做,也许可以使用干runSee和侧边栏上的其他问题,搜索加权/有偏随机数生成器。您调试过了吗?你验证过随机数是你期望的吗?我的错误,我把实际代码搞砸了。我明白了。谢谢你们抽出时间。