The first thing to note is that you don't need to make a new Random
each time you want a random number. Random
is seeded from the current time, so it's very likely that the code would run quickly enough that you get three of the same random number. As for adding the values to a list, there are a few different options for this. If you're always going to put items with three grouped values into the list, you could use a Tuple<int, int, int>
. Here's an example of that:
Random rnd = new Random();
int throw1 = rnd.Next(1, 18);
int throw2 = rnd.Next(1, 12);
int throw3 = rnd.Next(1, 6);
List<Tuple<int, int, int>> rolls = new List<Tuple<int, int, int>>();
rolls.Add(new Tuple<int, int, int>(throw1, throw2, throw3));
Or, you could have a nested list of integers, replacing the last two lines with:
List<List<int>> rolls = new List<List<int>>();
rolls.Add(new List<int> { throw1, throw2, throw3 });
Finally, you could also use an array:
List<int[]> rolls = new List<int[]>();
rolls.Add(new int[] { throw1, throw2, throw3 });
Another option is to make a custom class which holds your dice roll values (let's call it MultiDiceRoll
) and have a list of that (i.e. List<MultiDiceRoll>
).
Edit:
If you want to use ArrayList
(I would not advise it, for reasons I'll go into), you can do the following:
ArrayList rolls = new ArrayList();
rolls.Add(new int[] { throw1, throw2, throw3 }); // Or whatever other permutation you want
The reason I'd advise not doing this is because the items inside an ArrayList
are object
s. That is to say, ArrayList
doesn't use generics the way a List<T>
does. This means that every time you access an item in the list and you want to use it in some way, you have to cast it to the type you want (e.g. (int[])rolls[0]
). If you change the type of the list items, you then have a lot of type references you need to clean up and change as well.