为随机枚举对象分配概率 [英] Assigning probability to a random enum object
问题描述
所以我有一个枚举:
public enum Party {
DEMOCRAT, INDEPENDENT, REPUBLICAN
}
我目前有三个类之一:
public class ElectoralCollege {
public static final String FILE = "Electoral201X.txt";
private ArrayList <State> stateVotes;
Random rand = new Random();
public ElectoralCollege() throws IOException {
stateVotes = new ArrayList<State>();
assignStates();
}
public void assignStates() throws IOException {
File f = new File(FILE);
Scanner fReader = new Scanner(f);
while(fReader.hasNext()) {
String stateData = fReader.nextLine();
int stateEnd = stateData.indexOf(" - ");
String stateName = stateData.substring(0, stateEnd);
String stateVotes = stateData.substring(stateEnd + 2);
//System.out.println(stateName + " " + stateVotes);
}
这里我正在从一个文件有州名称和选举人数如下:佛罗里达州 - 29,所以这些都是想出来的。
Here I am reading from a file that has state names and their number of electoral votes as follows "Florida - 29", so that's all figured out.
接下来我要做的是使用随机对象从我们党的枚举中指定一个派对。共和党和民主党必须有2/5的获胜机会,而独立必须有1/5的机会。然后我必须创建一个State对象(将状态名称,投票数和派对作为参数),并将其抛入该数组列表中。最有可能为每个循环使用一个循环,只需要做一些更多的研究。
What I have to do next is use a random object to assign a party to them from my Party enum. Republican and Democrat must have a 2/5 chance of winning...while Independent must have a 1/5 chance. Then I must create a State object (which takes the state name, number of votes, and the party in as parameters) and toss it into that arraylist. Most likely going to use a for each loop for that, just need to do some more research on that.
我的问题是如何使用这个随机对象rand设定这三方的概率,并执行吗?任何人都有任何想法?
My question is how do I use this random object rand with a set probability for those three parties, and execute it? Anyone got any ideas?
编辑:底线是:如何为这三方实施2/5和1/5的概率,然后调用随机反对我根据这些概率给我一个派对?
Bottom line is: How do I implement a 2/5 and a 1/5 probability for those three Parties, and then call the random object to give me a party based on those probabilities?
在我的答案之后,我做到了:
AFTER mre's Answer, I did this:
Random rand = new Random();
List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);
稍后在....
public void assignStates()throws IOException {
public void assignStates() throws IOException {
File f = new File(FILE);
Scanner fReader = new Scanner(f);
while(fReader.hasNext()) {
String stateData = fReader.nextLine();
int stateEnd = stateData.indexOf(" - ");
String stateName = stateData.substring(0, stateEnd);
String numVote = stateData.substring(stateEnd + 2);
Party winner = parties.get(rand.nextInt(5));
//System.out.println(stateName + " " + numVote + " " + winner);
State voteInfo = new State(stateName, Integer.parseInt(numVote.trim()), winner);
stateVotes.add(voteInfo);
}
}
已回答,新问题:使用foreach循环从数组列表中添加值,然后使用访问器打印它们
推荐答案
拥有5个派对
实例的集合,其中2个为 DEMOCRAT
,2为 REPUBLICAN
,1是 INDEPENDENT
,然后使用随机数生成器生成随机索引(即0-4)用于访问例如
have a collection of 5 Party
instances, where 2 are DEMOCRAT
, 2 are REPUBLICAN
, and 1 is an INDEPENDENT
, and then use the random number generator to generate a random index (i.e. 0-4) for accessing e.g.,
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Demo
{
public static void main(String[] args)
{
Random r = new Random();
List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);
System.out.println(parties.get(r.nextInt(parties.size())));
}
enum Party
{
DEMOCRAT,
REPUBLICAN,
INDEPENDENT;
}
}
这篇关于为随机枚举对象分配概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!