1

I have repetition in loop - when isAllowed = true - then continue next iteration, but when isAllowed is false - then the program should repeat generate random positions, coordinates and check if the isAllowed until is true.

How to make recursion in that loop to achieve this objective?

foreach (var fleet in groupedFleetsbySpaceshipCounts)
{
    var randomPositionX = new Random().Next(0, 9);
    var randomPositionY = new Random().Next(0, 9);

    var cords = int.Parse(randomPositionX.ToString() + randomPositionY);
    map.Location[cords].ActualFleetPosition = fleet.Key[i];
    var isAllowed = IsPositionAllowed(map, cords);
    if (isAllowed)
    {
        break;
    }
    else
    {
        randomPositionX = new Random().Next(0, 9);
        randomPositionY = new Random().Next(0, 9);

        cords = int.Parse(randomPositionX.ToString() + randomPositionY);
        map.Location[cords].ActualFleetPosition = fleet.Key[i];
        isAllowed = IsPositionAllowed(map, cords);
        if (isAllowed)
        {
            break;
        }
        else
        {
            //recursion
        }
    }
    i++;
}
3
  • 3
    var randomPositionX = new Random().Next(0, 9); var randomPositionY = new Random().Next(0, 9); don't do this, declare the randoms outside the scope you have them as static (class level) and then use them in your calling code. Commented Aug 18, 2021 at 17:56
  • I suggest you should use simple for and move/increase the iterator/indexer when you want to do the next iteration unless it automatically repeats the loop. Commented Aug 18, 2021 at 17:58
  • 1
    You should not create new Random instances in a loop - you also do not need distinct instances to generate 2 values. Commented Aug 18, 2021 at 18:01

2 Answers 2

1

Maybe you want something like this:

var random = new Random();
foreach (var fleet in groupedFleetsbySpaceshipCounts)
{
    int coords;
    do {
        int randomPositionX = random.Next(0, 9);
        int randomPositionY = random.Next(0, 9);

        coords = 10 * randomPositionX + randomPositionY;
    } while (!IsPositionAllowed(map, coords));

    map.Location[coords].ActualFleetPosition = fleet.Key[i++];
}

I.e., you don't need a recursion (i.e., a method that calls itself), but another loop nested inside the first one.

Note that the max value of random.Next is exclusive. So, if you want numbers up to 9 you must use an upper value of 10. Also, if the lower value is 0, you can simply write random.Next(10).

It is important to create the Random object only once, because otherwise you might end up having different random objects generating the same pseudo random sequence. Using a static field in your class is even a better option than using a local variable

private static readonly _random = new Random();
Sign up to request clarification or add additional context in comments.

4 Comments

yes - exactly - it solve the issue - some syntax improvement and it will work
But why here is operation = 10 * randompositionX + randomPositionY? The intention here - concat as string
example if a = 5 and b = 7 - then cords should be 57, but at all it doesn't matter - coords should be between 0 and 99
Well, 10 * 5 + 7 = 57. Isn't it? If you have coords up to 99 then use 100 * x + y (= 507). However, you could use a 2-dimensional array instead: T[,] a = new T[With, Height]; and access it with a[x, y]. that would be easier.
0

I guess this should do the trick (edited with some of your comments, I didn't want to enter on other things rather than the question):

var random = new Random();    
foreach (var fleet in groupedFleetsbySpaceshipCounts)
    {
        do
        {
            var randomPositionX = random.Next(0, 9);
            var randomPositionY = random.Next(0, 9);

            //Im not sure how you're managing this 'coords' map, but gonna just go to the point 
            var cords = int.Parse(randomPositionX.ToString() + randomPositionY);
            map.Location[cords].ActualFleetPosition = fleet.Key[i];
        } while (!IsPositionAllowed(map, cords));
        i++;
    }

If you want 'cords' variable to be available outside the do...while loop you can safely declare it outside the loop (even better, outside the foreach).

2 Comments

You are sure? - the cords variable is inside scope, so it's undefined outside {} in do while
your solution worked as base - thank You! - there is need to define cords one level up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.