Skip to content

Commit 8f96282

Browse files
refactor: create random utils to replace rand()
1 parent f64cdec commit 8f96282

File tree

10 files changed

+46
-32
lines changed

10 files changed

+46
-32
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ add_executable(
1818
block/types_of_block.h
1919
camera/Camera.cpp
2020
camera/Camera.h
21+
utils/Utils.cpp
22+
utils/Utils.h
2123
window/Window.cpp
2224
window/Window.h
2325
player/Player.cpp

chunk/Chunk.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "../structures/Cloud.h"
1616
#include "../structures/SnowFlake.h"
1717
#include "../main.h"
18+
#include "../utils/Utils.h"
1819

1920

2021
Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
@@ -24,7 +25,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
2425

2526

2627
// PerlinNoise Setup
27-
uint32_t seed = rand();
28+
uint32_t seed = utils::random(RAND_MAX);
2829
double frequency = 0.1;
2930
int octaves = 4;
3031
const siv::PerlinNoise perlin(seed);
@@ -123,7 +124,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
123124
key = std::to_string(this->x + 1) + "_" + std::to_string(this->z);
124125
if (worldPtr->chunks.count(key) > 0) {
125126
if (this->biome != worldPtr->chunks[key]->biome) {
126-
int random_number = rand() % 100;
127+
int random_number = utils::random(RAND_MAX) % 100;
127128
int prob_actual_next_ground = 0;
128129

129130
if (i == CHUNK_SIZE - 1) prob_actual_next_ground = 80;
@@ -138,7 +139,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
138139
key = std::to_string(this->x) + "_" + std::to_string(this->z + 1);
139140
if (worldPtr->chunks.count(key) > 0) {
140141
if (this->biome != worldPtr->chunks[key]->biome) {
141-
int random_number = rand() % 100;
142+
int random_number = utils::random(RAND_MAX) % 100;
142143
int prob_actual_next_ground = 0;
143144

144145
if (j == CHUNK_SIZE - 1) prob_actual_next_ground = 80;
@@ -153,7 +154,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
153154
key = std::to_string(this->x - 1) + "_" + std::to_string(this->z);
154155
if (worldPtr->chunks.count(key) > 0) {
155156
if (this->biome != worldPtr->chunks[key]->biome) {
156-
int random_number = rand() % 100;
157+
int random_number = utils::random(RAND_MAX) % 100;
157158
int prob_actual_next_ground = 0;
158159

159160
if (i == 0) prob_actual_next_ground = 80;
@@ -168,7 +169,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
168169
key = std::to_string(this->x) + "_" + std::to_string(this->z - 1);
169170
if (worldPtr->chunks.count(key) > 0) {
170171
if (this->biome != worldPtr->chunks[key]->biome) {
171-
int random_number = rand() % 100;
172+
int random_number = utils::random(RAND_MAX) % 100;
172173
int prob_actual_next_ground = 0;
173174

174175
if (j == 0) prob_actual_next_ground = 80;
@@ -212,13 +213,6 @@ void Chunk::render() {
212213
}
213214
}
214215

215-
int Chunk::random(int max) {
216-
std::random_device seed_gen;
217-
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
218-
std::uniform_int_distribution<> dist(0, max);
219-
return dist(engine);
220-
}
221-
222216
int Chunk::getBlock(int x, int y, int z) {
223217
return this->blocks[x][y][z];
224218
}
@@ -251,7 +245,7 @@ void Chunk::generateStructures() {
251245

252246
// Get surface_height
253247
int surface_height = this->heights[i][j];
254-
int random_number = this->random(100);
248+
auto random_number = utils::random(100);
255249

256250
// Trees
257251
if (random_number < this->biome->tree_frequency * 100) {
@@ -276,7 +270,7 @@ void Chunk::generateStructures() {
276270
// Cloud
277271
for (int i = 5; i < CHUNK_SIZE - 5; ++i) {
278272
for (int j = 5; j < CHUNK_SIZE - 5; ++j) {
279-
int prob = this->random(1000);
273+
int prob = utils::random(1000);
280274
if (prob < 50) {
281275
int surface_height = 0;
282276
for (int k = 0; k < CHUNK_HEIGHT; ++k) {

chunk/Chunk.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class Chunk {
2828

2929
void render();
3030

31-
int random(int max);
32-
3331
int getBlock(int x, int y, int z);
3432

3533
int getHeight(int x, int z);

structures/Cloud.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#include "Cloud.h"
6+
#include "../utils/Utils.h"
67
#include <random>
78

89

@@ -12,7 +13,7 @@ namespace {
1213
}
1314

1415
Cloud::Cloud(int x, int y, int z, Chunk *chunk) {
15-
int radius_cloud = MIN_RADIUS_CLOUD + Cloud::random(MAX_RADIUS_CLOUD - MIN_RADIUS_CLOUD);
16+
int radius_cloud = MIN_RADIUS_CLOUD + utils::random(MAX_RADIUS_CLOUD - MIN_RADIUS_CLOUD);
1617

1718
for (int i = -radius_cloud; i < radius_cloud; ++i) {
1819
for (int j = -radius_cloud; j < radius_cloud; ++j) {
@@ -23,10 +24,3 @@ Cloud::Cloud(int x, int y, int z, Chunk *chunk) {
2324
}
2425
}
2526
}
26-
27-
int Cloud::random(int max) {
28-
std::random_device seed_gen;
29-
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
30-
std::uniform_int_distribution<> dist(0, max);
31-
return dist(engine);
32-
}

structures/Cloud.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
class Cloud {
1111
public:
1212
explicit Cloud(int x, int y, int z, Chunk *chunk);
13-
14-
static int random(int max);
1513
};
1614

1715

structures/Rock.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#include "Rock.h"
6+
#include "../utils/Utils.h"
67
#include <random>
78

89
Rock::Rock(int x, int y, int z, Chunk *chunk) {
@@ -13,10 +14,10 @@ Rock::Rock(int x, int y, int z, Chunk *chunk) {
1314
for (int i = -2; i < 2; ++i) {
1415
for (int j = -2; j < 2; ++j) {
1516

16-
int random_number = rand() % 100;
17+
int random_number = utils::random(RAND_MAX) % 100;
1718
if (random_number < proba) {
1819
chunk->blocks[x + i][chunk->heights[x + i][z + j] + 1][z + j] = ROCK;
19-
random_number = rand() % 100;
20+
random_number = utils::random(RAND_MAX) % 100;
2021
if (random_number < proba_mount) {
2122
chunk->blocks[x + i][chunk->heights[x + i][z + j] + 2][z + j] = ROCK;
2223
}

structures/Tree.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#include "Tree.h"
6+
#include "../utils/Utils.h"
67
#include <random>
78
#include <cmath>
89

@@ -17,7 +18,7 @@ namespace {
1718

1819
Tree::Tree(int x, int y, int z, Chunk *chunk) {
1920

20-
int height_tree = MIN_HEIGHT_TREE + rand() % (MAX_HEIGHT_TREE - MIN_HEIGHT_TREE);
21+
int height_tree = MIN_HEIGHT_TREE + utils::random(RAND_MAX) % (MAX_HEIGHT_TREE - MIN_HEIGHT_TREE);
2122

2223
for (int i = 0; i < height_tree; ++i) {
2324
if (chunk->blocks[x][y + i][z] == AIR) {
@@ -26,7 +27,7 @@ Tree::Tree(int x, int y, int z, Chunk *chunk) {
2627
}
2728

2829

29-
int radius_tree = MIN_RADIUS_TREE + rand() % (MAX_RADIUS_TREE - MIN_RADIUS_TREE);
30+
int radius_tree = MIN_RADIUS_TREE + utils::random(RAND_MAX) % (MAX_RADIUS_TREE - MIN_RADIUS_TREE);
3031
int texture = Tree::chooseTexture();
3132

3233
for (int i = -radius_tree + 1; i <= radius_tree - 1; i++) {
@@ -69,7 +70,7 @@ Tree::Tree(int x, int y, int z, Chunk *chunk) {
6970
}
7071

7172
int Tree::chooseTexture() {
72-
int num = rand() % 100;
73+
int num = utils::random(RAND_MAX) % 100;
7374
int texture = 0;
7475
if (0 <= num && num < 33) {
7576
texture = TREELEAVES;

utils/Utils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Created by Keita Nonaka on 3/28/22.
3+
//
4+
5+
#include <random>
6+
#include "Utils.h"
7+
8+
int utils::random(int max) {
9+
std::random_device seed_gen;
10+
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
11+
std::uniform_int_distribution<> dist(0, max);
12+
return dist(engine);
13+
}

utils/Utils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Created by Keita Nonaka on 3/28/22.
3+
//
4+
5+
#ifndef PROJECT_UTILS_H
6+
#define PROJECT_UTILS_H
7+
8+
namespace utils {
9+
int random(int max);
10+
}
11+
12+
#endif //PROJECT_UTILS_H

world/World.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cmath>
88
#include "World.h"
99
#include "../main.h"
10+
#include "../utils/Utils.h"
1011

1112
World::World() {
1213
this->chunks = std::map<std::string, Chunk *>();
@@ -139,7 +140,7 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
139140

140141

141142
auto it = possibleBiomeTypes.begin();
142-
advance(it, rand() % possibleBiomeTypes.size());
143+
advance(it, utils::random(RAND_MAX) % possibleBiomeTypes.size());
143144
Biome biome(*it, chunk_x, chunk_y, this->biome_types[*it]);
144145
this->biomes.push_back(biome);
145146
return this->biome_types[*it];

0 commit comments

Comments
 (0)