-1

I'm new to C#. Is there any way that I can put the roll1 variable value into the rolls arrayList? Do I need to put this section within a method separate to the main?

Random diceRoll1 = new Random();

int throw1 = diceRoll1.Next(1, 18);

Random diceRoll2 = new Random();

int throw2 = diceRoll2.Next(1, 12);

Random diceRoll3 = new Random();

int throw3 = diceRoll3.Next(1, 6);

ArrayList rolls = new ArrayList();

int roll1 = Console.WriteLine(throw1 + throw2 + throw3);
Console.ReadLine();
Rowan Reed
  • 29
  • 1
  • 4
  • 1
    Possible duplicate of [Adding values to a C# array](https://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array) – Twisty Dec 18 '17 at 23:56
  • No, because i want to extract to value from the variable – Rowan Reed Dec 18 '17 at 23:57
  • Then try https://stackoverflow.com/questions/15640643/c-sharp-arraylist-add-method-adds-ref-to-items – Twisty Dec 18 '17 at 23:59
  • 2
    You don't need to create a new random variable each time, in fact you shouldn't. If the code runs quickly enough, you could get the same "random" number three times, as the random number generator is seeded from the current time. Use a single `Random` and call `Next()` (with your args) on it three times. – Lauraducky Dec 19 '17 at 00:02
  • 1
    It's also a little unclear what you're asking. Do you want all three integer values grouped as a single list item? Or do you want them each added as a single list item? – Lauraducky Dec 19 '17 at 00:07
  • `Console.WriteLine` returns `void`, so this doesn't even compile. Maybe you wanted to do `int roll1 = Throw1 + Throw2 + Throw3; Console.WriteLine(roll1);`? – Camilo Terevinto Dec 19 '17 at 00:18
  • @Lauraducky yes i want to put all three integer values into one value then have that go into a list. – Rowan Reed Dec 19 '17 at 00:32

2 Answers2

1

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 objects. 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.

Lauraducky
  • 674
  • 11
  • 25
  • This is great thanks. Could I loop this process to create multiple lists within the main list? – Rowan Reed Dec 19 '17 at 01:02
  • @RowanReed You can indeed. You don't need to create a new list each time, just store a reference to it and add to it as needed. – Lauraducky Dec 19 '17 at 01:04
  • The same goes for the `Random` by the way. You can store a reference to it to avoid recreating it each time. – Lauraducky Dec 19 '17 at 01:06
  • What about getting this data back out to display it? – Rowan Reed Dec 19 '17 at 01:07
  • If you want to display all of the rolls so far, loop through the list and print each one. You could also index into the list to get specific items (e.g. `rolls[rolls.Count - 1]` is the last item added). Take a look at `string.Format()` to look at how to format display strings. – Lauraducky Dec 19 '17 at 01:12
  • The question asks about `ArrayList` (for reasons... who knows). This answer does not provide actual answer to the question as asked - consider editing it. – Alexei Levenkov Dec 19 '17 at 01:15
  • 1
    @AlexeiLevenkov thanks for the catch. I've edited my answer to address this. – Lauraducky Dec 19 '17 at 01:24
-1

You could initialize a list of type of int and add each element into that list. After adding all elements, you can loop through that list and print it. https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx