Help With Random Numbers

Hello, I have this piece of code here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Generate_Square_Room_Randomly(int rows, int columns, char world[]) {
     srand ( time(NULL) );
     int room_hieght = rand() % 5 + 8;
     int room_width = rand() % 5 + 8;
     int bounds_hieght = 6;
     int bounds_width = 6;
     int centre_row = rand() % 6 + (rows - bounds_hieght);
     int centre_column = rand() % 6 + (columns - bounds_width);
     world[columns*centre_row+centre_column] = 'C';
     //Right Wall
     for (int i = 0; i < room_hieght; i++) {
         world[columns*(centre_row)+centre_column-i] = '#';
         }
     //Top Wall
     for (int i = 0; i < room_width; i++) {
         world[columns*(centre_row-i)+centre_column] = '#';
         }
     //Left Wall
     for (int i = 0; i < room_hieght; i++) {
         world[columns*(centre_row-room_width)+centre_column-i] = '#';
         }
     //Bottom Wall
     for (int i = 0; i < room_width; i++) {
         world[columns*(centre_row-i)+centre_column - room_hieght +1] = '#';
         }
     }


However, whenever I run it the room always appears to be placed in a random location near the bottom right corner, and never anywhere else, I think its the way I do the bounds, but Ive tried to change it and found no answer

Help?

Thanks

EDIT: Also, each time I use rand(), do I need to seed it beforehand, or will once for all work as I have used above?
Last edited on
I have some ideas on this

First of all, I may need more information on what you input for rows and columns. If your rows is big, like 255, then your centre_row ranges from 249-254. I suppose centre_row means the horizontal location for the room, which is near the right side.

For the question about rand(), doing it once is like calling the rand sequence from the a constant number. I suggest that you srand everytime before rand()ing.

I am not sure my understanding in rand() is right, please correct me if i wrote anything wrong and I'll be happy to know.
For the question about rand(), doing it once is like calling the rand sequence from the a constant number. I suggest that you srand everytime before rand()ing.

I am not sure my understanding in rand() is right, please correct me if i wrote anything wrong and I'll be happy to know.


This is the opposite. Only seed once in your program.

As for your problem, it's because your bounds_width/height are simply an offset from the top/left side, not all of the sides like I think you intended.

I would just make a rand_int() function so you don't have to deal with the rand() stuff manually.
firedraco wrote:
Only seed once in your program.
Before the first call to rand().
Only seed once in your program.

Thanks for clearing that for me.
Ok, redid the code a bit and fixed some problems confusing X and Y, however, i still get it not responding some times, here is the new code, I will have a look at it but any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

//========================//
//Array Format: Y*Width+X //
//========================//
void Game_Start(int x, int y, char world[]) {
     for (int i = 0; i < x*y; i++) {
         world[i] = '.';
         }
     }

void Map_Print(int x, int y, char world[]) {
     ofstream map_save;
     map_save.open("Map_Save.txt");
     for (int i = 0; i < y; i++) {
         for (int j = 0; j < x; j++) {
             map_save << world[i*x+j];
             }
             map_save << endl;
         }
     map_save.close();
     }

void Generate_Square_Room_Randomly(int x, int y, char world[]) {
     int room_hieght = rand() % 12 + 6;
     int room_width = rand() % 12 + 6;
     int bottomcorner_x = rand() % x + 19;
     int bottomcorner_y = rand() % y + 19;
     //Right Wall
     for (int i = 0; i < room_hieght; i++) {
         world[(bottomcorner_y+i)*x+bottomcorner_x] = '#';
         }
     //Top Wall
     for (int i = 0; i < room_width; i++) {
         world[bottomcorner_y*x+(bottomcorner_x-i)] = '#';
         }
     //Left Wall
     for (int i = 0; i < room_hieght; i++) {
         world[(bottomcorner_y+i)*x+(bottomcorner_x-room_width)] = '#';
         }
     //Bottom Wall
     for (int i = 0; i < room_width; i++) {
         world[(bottomcorner_y+room_hieght-1)*x+(bottomcorner_x-i)] = '#';
         }
     }
int main(int argc, char *argv[])
{
    //=======================================================//
    //Naming Convention: X = Left and Right, Y = Up and Down //
    //=======================================================//
    srand(time(NULL));
    int rows = rand() % 40 + 100;
    int columns = rand() % 40 + 100;
    char world[rows*columns];
    Game_Start(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Generate_Square_Room_Randomly(rows,columns,world);
    Map_Print(rows,columns,world);
    system("PAUSE");
    return EXIT_SUCCESS;
}



EDIT: Changed This Line

1
2
     int bottomcorner_x = rand() % x + 19;
     int bottomcorner_y = rand() % y + 19;


To This:

1
2
     int bottomcorner_x = rand() % (x - 20) + 19;
     int bottomcorner_y = rand() % (y - 20) + 19;


and it worked alot better but still stops working a couple of times
Last edited on
Topic archived. No new replies allowed.