0

Here's what I'm trying to do: I have a class called RandomRoomGenerator, which has a method called GenerateRoom. This GenerateRoom method is called inside my main method based on the grid size(the width or height) multiplied by itself to get the total number of rooms. The method itself creates a new room with parameters chosen randomly from my ItemDB and RandomRoomValues. The problem is, I need a way to generate the room's position without it overlapping previously generated rooms. So far I am only able to return 1 random value, which is copied however many times over and over again. Everything else is fine, but I would like a bit of clarification on how Random works in C#. In my GenerateRandomItems method, when I set the first .Next() parameter to 0, it would never return an item, no matter how large the max value was. Here's my RandomRoomGenerator class:

public class RandomRoomGenerator
{
    public Room GenerateRoom(int gridSize, ref List<Room> rooms)
    {
        RandomRoomDescriptions randomRoomDescriptions = new RandomRoomDescriptions();
        ItemDB itemDb = new ItemDB();

        string description = randomRoomDescriptions.roomDescriptions[new Random().Next(0, randomRoomDescriptions.roomDescriptions.Count)];

        Room room = new Room(description, GenerateXPos, GenerateYPos, new Random().Next(1, 7), new Random().Next(1, 7), new Random().Next(1, 7), GenerateRandomItems(itemDb));
        return room;
    }





    private List<Item> GenerateRandomItems(ItemDB itemDb)
    {
        List<Item> items = new List<Item>();

        for (int i = 0; i < new Random().Next(0, 2); i++)
        {
            items.Add(itemDb.healItems[new Random().Next(0, itemDb.healItems.Count)]);
        }

        for (int i = 0; i < new Random().Next(0, 2); i++)
        {
            items.Add(itemDb.cleanseItems[new Random().Next(0, itemDb.cleanseItems.Count)]);
        }

        for (int i = 0; i < new Random().Next(0, 2); i++)
        {
            items.Add(itemDb.foodItems[new Random().Next(0, itemDb.foodItems.Count)]);
        }

        for (int i = 0; i < new Random().Next(0, 2); i++)
        {  
            items.Add(itemDb.drinkItems[new Random().Next(0, itemDb.drinkItems.Count)]);
        }

        return items;
    }
}
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. – Progman May 24 '20 at 07:08
  • Keep in mind that `i < new Random().Next(0, 2)` is evaluated in each `for` loop iteration. So in one iteration you might check if `i < 1` and in the next iteration you might check `i < 0`. That's not very intuitive to use. Also, you don't need to build a new random generator all the time. Instead, instance only one `Random` instance and pass this random generator instance around the methods where you need it. Also, use properties or methods to access data from other classes, see https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property – Progman May 24 '20 at 07:11

0 Answers0