分配概率的随机枚举对象 [英] Assigning probability to a random enum object
问题描述
所以,我在这里有一个枚举:
So I have an enum here:
public enum Party {
DEMOCRAT, INDEPENDENT, REPUBLICAN
}
和我目前有这三个类别之一:
and I currently have this, one of three classes:
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的机会。然后,我必须创建国家对象(以国家名义,投票数和作为参数的一方),并将其扔进了数组列表。最有可能会使用一个for each循环的是,仅仅需要做一些更多的研究上。
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.
我的问题是我怎么使用此随机对象兰特为那些三方一套概率,并执行它?任何人有什么想法?
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?
在MRE的答案,我这样做:
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);
和稍后上....
公共无效assignStates()抛出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);
}
}
回答,新问题:<一href=\"http://stackoverflow.com/questions/15048973/using-a-foreach-loop-to-add-values-from-an-arraylist-and-then-print-them-using/15049027#comment21155196_15049027\">Using foreach循环,从一个ArrayList添加值,然后利用访问打印出来
推荐答案
有5 党
实例的集合,2是民主党
,2个共和党
,1个是独立
,然后用随机数发生器生成用于访问如随机指数(即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屋!