Skip to content

Commit

Permalink
Add random number engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Mar 16, 2017
1 parent 0578c96 commit 224ea5e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Minecraft.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<Unit filename="Source/Texture/Texture.h" />
<Unit filename="Source/Texture/Texture_Atlas.cpp" />
<Unit filename="Source/Texture/Texture_Atlas.h" />
<Unit filename="Source/Util/Random.cpp" />
<Unit filename="Source/Util/Random.h" />
<Unit filename="Source/World/Block/Block_Data.cpp" />
<Unit filename="Source/World/Block/Block_Data.h" />
<Unit filename="Source/World/Block/Block_Database.cpp" />
Expand Down
2 changes: 1 addition & 1 deletion Source/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Display.h"

#include "States/Playing.h"
#include "States/SPlaying.h"

Application::Application()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "States/Game_State.h"

#include "Renderer/Master.h"
#include "Renderer/RMaster.h"

#include "Entity.h"
#include "Camera.h"
Expand Down
24 changes: 24 additions & 0 deletions Source/Util/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Random.h"


namespace Random
{
std::mt19937_64 randomEngine;

void init()
{
randomEngine.seed(std::time(nullptr));
}

int64_t intInRange(int64_t low, int64_t high)
{
std::uniform_int_distribution<int64_t> dist (low, high);
return dist(randomEngine);
}

float floatInRange(float low, float high)
{
std::uniform_real_distribution<float> dist (low, high);
return dist(randomEngine);
}
}
50 changes: 50 additions & 0 deletions Source/Util/Random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef RANDOM_H_INCLUDED
#define RANDOM_H_INCLUDED

#include <random>
#include <cstdint>
#include <ctime>

namespace Random
{
void init();

int64_t intInRange(int64_t low, int64_t high);
float floatInRange(float low, float high);

template<typename Engine>
class Generator
{
public:
Generator()
{
m_randomEngine.seed(std::time(nullptr));
}

int64_t intInRange(int64_t low, int64_t high)
{
std::uniform_int_distribution<int64_t> dist (low, high);
return dist(m_randomEngine);
}

float floatInRange(float low, float high)
{
std::uniform_real_distribution<float> dist (low, high);
return dist(m_randomEngine);
}

template<typename T>
T numberInRange(T low, T high)
{
std::uniform_real_distribution<T> dist (low, high);
return dist(m_randomEngine);
}

private:
Engine m_randomEngine;
};


}

#endif // RANDOM_H_INCLUDED
14 changes: 14 additions & 0 deletions Source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
#include "Display.h"

#include "Application.h"
#include "Util/Random.h"

#include <iostream>

int main()
{
Random::init();

Random::Generator<std::minstd_rand> randomGen;

for (int i = 0; i < 10; i++)
{
std::cout << randomGen.numberInRange(5.0, 8.0) << std::endl;
}

/*
Display::init();
Application app;
app.runMainGameLoop();
return 0;
*/
}

0 comments on commit 224ea5e

Please sign in to comment.