philsupertramp/game-math
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1#pragma once
2
3#include "vec/vec2.h"
4#include "vec/vec3.h"
5#include "vec/vec4.h"
6#include <ctime>
7#include <random>
8
9static bool TimeInitialized = false;
10static int RandomSeed = 2;
14class Random
15{
20 static void InitTime(bool useSeed = true) {
21 std::srand(useSeed ? RandomSeed : (int)time(0));
22 TimeInitialized = true;
23 }
24
25public:
30 static void SetSeed(int seed) {
31 RandomSeed = seed;
32 TimeInitialized = false;
33 }
40 static double Get(double l = 0.0, double r = 1.0) {
41 if(!TimeInitialized) InitTime(false);
42
43 std::random_device rd; // create object for seeding
44 std::mt19937 gen{ rd() }; // create engine and seed it
45 std::uniform_real_distribution<> dist(l, r); // create distribution for integers with [1; 9] range
46 return dist(gen);
47 }
55 template<typename T>
56 static vec2<T> Get(double l = 0.0, double r = 1.0) {
57 if(!TimeInitialized) InitTime(false);
58 return vec2<T>(Get(), Get());
59 }
67 template<typename T>
68 static vec3<T> Get(double l = 0.0, double r = 1.0) {
69 if(!TimeInitialized) InitTime(false);
70 return vec3<T>(Get(), Get(), Get());
71 }
79 template<typename T>
80 static vec4<T> Get(double l = 0.0, double r = 1.0) {
81 if(!TimeInitialized) InitTime(false);
82 return vec4<T>(Get(), Get(), Get(), Get());
83 }
84};
static int RandomSeed
Definition: Random.h:10
static bool TimeInitialized
Definition: Random.h:9
Definition: Random.h:15
static void InitTime(bool useSeed=true)
Definition: Random.h:20
static vec4< T > Get(double l=0.0, double r=1.0)
Definition: Random.h:80
static double Get(double l=0.0, double r=1.0)
Definition: Random.h:40
static vec2< T > Get(double l=0.0, double r=1.0)
Definition: Random.h:56
static void SetSeed(int seed)
Definition: Random.h:30
static vec3< T > Get(double l=0.0, double r=1.0)
Definition: Random.h:68
Definition: vec2.h:11
Definition: vec3.h:14
Definition: vec4.h:11