-1

I was trying to start a small piece of code that is supposed to roll 5 dice. 4 of the dice are supposed to have the numbers from dietype1 (2,3,4,5,6,10) and the 5th is supposed to have the numbers from dietype2 (1,2,4,5,6,10). I am lost on how to execute a random selection. Some of the examples I have looked up use Random or some other method that I am not familiar with.

I also had a question about the way I set up the two different types of dice. Would using a list be the easiest choice to go with or would an ArrayList make more sense?

Any input on how to guide me or set up the lists better is greatly appreciated!

Here's what I have so far...

public class inc1{
   private int die1;
   private int die2;
   private int die3;
   private int die4;
   private int die5;
   List<Integer> dietype1 = Arrays.asList(2, 3, 4, 5, 6, 10);
   List<Integer> dietype2 = Arrays.asList(1, 2, 4, 5, 6, 10);

   public void roll(){
   }

   public static void main(String[] args){
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • ArrayList is a type of List. – shmosel Feb 02 '18 at 22:42
  • 1
    What is this "die1",...,"die5" thing doing... Don't repeat yourself. – Andrey Tyukin Feb 02 '18 at 22:44
  • I assume the dice are independent of each other in terms of what they might turn up when rolled. So you just need to pick a random value for each one and put that random value into its corresponding array location to get a complete random list. You'll need to take the results of the random library call and map it to however many values you have. Easiest way is if you have `N` items, just take the random integer module `N`. – lurker Feb 02 '18 at 22:44
  • Looks like someone had the same assignment: https://stackoverflow.com/questions/48591959/eclipse-does-not-recognize-my-return-function-in-java – Cardinal System Feb 02 '18 at 22:47

1 Answers1

2

I am sure you are familiar with List#get(int)? If so, this should be easy. Just use ThreadLocalRandom#nextInt(int):

dietype1.get(ThreadLocalRandom.current().nextInt(dietype1.size()));
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • 4
    Nothing screams "I've copied my homework from somewhere else" louder than a `ThreadLocalRandom`. ;) +1 – Andrey Tyukin Feb 02 '18 at 22:49
  • 2
    `nextInt()` expects an exclusive bound. – shmosel Feb 02 '18 at 22:49
  • 1
    @shmosel in this case, you are wrong. Click the link attached to `nextInt` in my answer. – Cardinal System Feb 02 '18 at 22:52
  • 2
    @CardinalSystem I would suggest you do the same. – shmosel Feb 02 '18 at 22:53
  • @DawoodibnKareem [`List#size()`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#size--) returns the number of elements in the list. In Java, indexing starts at zero, so if I didn't include -1 and ThreadLocalRandom returned the list size, than it would throw an index out of bounds exception. Unless nextInt returns between but not including the passed bounds and I simply forgot that... – Cardinal System Feb 02 '18 at 22:55
  • @DawoodibnKareem Ah, just [saw that](https://stackoverflow.com/a/363692/5645656). I was reading *inclusive* subconsciously :P – Cardinal System Feb 02 '18 at 22:58
  • OK, thank you for correcting your answer. – Dawood ibn Kareem Feb 02 '18 at 23:03