-1

I am trying to make a program that I Believe has to make use of recursion. In short: create a program that will throw a Dice. If it lands on a 6, two Dices are created and rolled, and so on until no more 6 are rolled.

The problem is not creating a new or true random object, but the recursive Dices.

The recursion method looks like this:

    public static int Recursion(int a)
    {
        Random m = new Random();

        if (a < 6)
        {
            return a;
        }

        else
        {
            a = m.Next(1, 7);
            return Recursion(a) * 2;
        }
    }
LV428
  • 3
  • 2
  • 1
    " If it lands on a 6, two Dices are created and rolled, and so on until no more 6 are rolled" - No more sixes by which dice(s)? – Bogdan Doicin May 12 '19 at 16:50
  • 2
    Welcome to stackoverflow. what is your question here? – Prasad Telkikar May 12 '19 at 16:50
  • 3
    Depending on what you are really trying to do, a simple loop might work in place of recursion. For certain, you do not need a new `Random` object each time - one can create an infinite number of random numbers – Ňɏssa Pøngjǣrdenlarp May 12 '19 at 16:56
  • Thank you. I am trying to make a game that if the first Dice hits 6, two more Dices are created (under the "else" statement). To make them unique I have put a new random instance (m.Next(1,7)), and then I am attempting to return the final value by throwing two new Dices. But it seems that each time I get a 6, I also get a Stack Overflow (too many recursions?). How to get around this? – LV428 May 12 '19 at 16:57
  • Possible duplicate of [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) –  May 12 '19 at 17:01
  • 3
    You are not doubling the number of dices, you are doubling the rolled number. – Olivier Jacot-Descombes May 12 '19 at 17:02
  • @NatPongjardenlarp - I can't see how a loop would work because new Dices are created the whole time? – LV428 May 12 '19 at 17:03
  • @elgonzo - Thank you, yes, I am familiar with "true" random so to speak. However, my main question is the recursive problem of how to "create" two new Dices each time one Dice lands on a 6. – LV428 May 12 '19 at 17:04
  • 1
    @LV428, i am afraid you are not familiar with this. Read the answer in the link i gave. Your problem is definitely related to this... Note that the clock value used as seed by the Random() constructor has only a certain resolution. Your recursive execution is way too fast and can recurse many times (too many times, causing a StackOverflowException) before the clock value and thus the seed of each random number generator you create would change. –  May 12 '19 at 17:05
  • The dices can be represented by a single number indicating their number (not to be confused with the rolled number). In a loop, you can get as many random numbers as you have dices. In an enclosing loop, you loop until no more 6 are rolled. – Olivier Jacot-Descombes May 12 '19 at 17:08

2 Answers2

1

Possibly something like this?

public static int Roll() {
   return Roll(new Random(), 1);
}

public static int Roll(Random random, int diceCount) {
    int sum = 0;
    for (int dice = 1; dice <= diceCount; ++dice) {
        int rolled = random.Next(1, 7);
        if (rolled == 6) {
            sum += Roll(random, 2)
        }
        else
        {
            sum += rolled;
        }

    }

    return sum;
}

So, first I roll one die/dice. If it is not 6, then I accept its value as the result. If it is six, then I remove that die/dice, and replace it with two other I roll. Then, for each of the new ones I follow the same rule, until all the dice on the table is rolled and none of them is 6. Now I sum all the value of dice. This is what this recursive algorithm does. Note, that - however it has infinitely low chance - you can play this until the end of times, since there is always chance of rolling 6, so you can possibly roll only 6's until you die.

0

You can make it more object oriented by creating a dice object:

using System;
using System.Collections.Generic;

class Dices
{
    public class Dice
    {
        private static Random roler = new Random();
        private int roledNumber;
        public int Role()
        {
            roledNumber = roler.Next(6) + 1 ;
            return roledNumber;
        }

        public int Number
        {
            get { return roledNumber; }
        }
    }

    static void Main(string[] args)
    {
        List<Dice> allDices = new List<Dice>();

        Dice firstDice = new Dice();
        allDices.Add(firstDice);

        if (firstDice.Role() == 6) createDices(allDices);
    }

    static void createDices(List<Dice> dices)
    {
        Dice first = new Dice();
        dices.Add(first);
        if (first.Role() == 6) createDices(dices);
        Dice second = new Dice();
        dices.Add(second);
        if (second.Role() == 6) createDices(dices);

    }
}
Aldert
  • 4,209
  • 1
  • 9
  • 23