I am creating a method where if you pass in a parameter of type Random, then it will return a random object. Here is basically what I am trying to do:
public T choose(Random r) {
int randomInt = r.nextInt(randomList.size()); // randomList is just a instance variable
return randomList.get(randomInt);
}
The random list has this the following strings:[2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a]
Then I made a driver with the following code
for (int i = 0; i < 10; i++) {
System.out.print(rndList.choose(rnd)); // rnd is initialized as a static Random variable
}
However my outputs are not coming out random. I used the debugger and found out that my choose method generates an integer that is relatively low, so it will always print out either 2's or 1's but never c's or a's. I can't figure out why this is happening and help would be greatly appreciated.
EDIT: Problem was solved. I left out alot of detail, but when I called the size() method, that was something I overwrote which had an error which would return a smaller number than I would have liked. Thanks to dtech for noticing my silly mistake. Thank you for everyone who tried to help me!
50times for example?Random()orRandom(someConstantIntegerValue)? If you use the latter, beware that you'll get the same "random" sequence each time you run the program.a1c211a121what do you get and what do you expect to get?