-2

I 'm creating a Text Base arena rpg where each day new monsters are add to the list, but only one monster get add to the list repeatly with the same stats which for some reason keeps increasing each day. What i need are that diferent objects with diferent values are created than add to a list, the second part works well.

This method calls the creator.

public static List<Monster> MonsterOfTheDay()
  {
    int count = 0;
    List<Monster> MonstersListOfTheDay = new List<Monster>();

    while(count <= 5)
    {
      MonstersListOfTheDay.Add(Creator());
      count++;
    }

    return MonstersListOfTheDay;
  }

This are the creator

public static Monster Creator()
  {
    Random random = new Random();
    Monster monsterChoosen = monsterListPrefab.Find(m => m.Id == random.Next(0, monsterListPrefab.Count -1));

    monsterChoosen.Level = random.Next(monsterChoosen.Level, monsterChoosen.Level + 3);

    //1Offensive, 2Defensive, 3Balance 
    monsterChoosen.Type = (Types)typeList.GetValue(random.Next(1, typeList.Length));

    Console.WriteLine("Estou Aqui");

    int atributes = monsterChoosen.Level * 3;
    int spend = 0;

    Console.WriteLine("Estou Aqui");
    while(spend != atributes)
    {
      int chance = random.Next(0, 100); 
      if(monsterChoosen.Type == Types.Offensive)
      {
        if(chance >= 0 && chance <= 60)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 61 && chance <= 70)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 71 && chance <= 85)
        {
          monsterChoosen.Agi++;
          spend++;
        }

        if(chance >= 86 && chance <= 100)
        {
          monsterChoosen.Vig++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Defensive)
      {
        if(chance >= 0 && chance <= 60)
        {
          monsterChoosen.Vig++;
          spend++;
        }

        if(chance >= 61 && chance <= 70)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 71 && chance <= 85)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 86 && chance <= 100)
        {
          monsterChoosen.Agi++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Balance)
      {
        if(chance >= 0 && chance <= 25)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 26 && chance <= 50)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 51 && chance <= 75)
        {
          monsterChoosen.Agi++;
          spend++;
        }

        if(chance >= 76 && chance <= 100)
        {
          monsterChoosen.Vig++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Prefab)
      {
        spend++;
      }
      else
      {
        Console.WriteLine("Error");
      }
    }

    return monsterChoosen;
  }
  • 1
    [Random.Next](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-5.0#System_Random_Next_System_Int32_System_Int32_) has second parameter as exclusive bound, so if `typeList` in your example contains 3 elements, `monsterChoosen.Type` will be in `[1..3)` so either 1 or 2. If `typeList` contains only 2 elements , Type always will be 1 – Renat Feb 26 '21 at 02:14
  • Closing as a typo – TheGeneral Feb 26 '21 at 04:16
  • In addition to other problems noted, you are creating a new `Random` object every time you want a new random number. This is wrong. See duplicate. – Peter Duniho Feb 26 '21 at 22:26

1 Answers1

0

I chance the initial 3 lines of the Creator method, now its working properly, the reason is, as Phillipp said in the game developer stack, the old code keep the information of the monsterChoosen variable and won't generate a new one when called again, keeping the same result and only increasing its stats and level, now with the new line of the variable, monsterChoosen can update every time its called.

Random random = new Random();
int randId = random.Next(2);

Monster monsterChoosen = new Monster(monsterListPrefab.Find(m => m.Id == randId));