forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNG.cpp
More file actions
121 lines (106 loc) · 2.93 KB
/
Copy pathRNG.cpp
File metadata and controls
121 lines (106 loc) · 2.93 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RNG.h"
#include <cmath>
#include <time.h>
#include <stdlib.h>
#ifndef UINT64_MAX
#define UINT64_MAX 0xffffffffffffffffULL
#endif
namespace OpenXcom
{
namespace RNG
{
/* Written in 2014 by Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
/* This is a good generator if you're short on memory, but otherwise we
rather suggest to use a xorshift128+ (for maximum speed) or
xorshift1024* (for speed and very long period) generator. */
uint64_t x = time(0); /* The state must be seeded with a nonzero value. */
uint64_t next()
{
x ^= x >> 12; // a
x ^= x << 25; // b
x ^= x >> 27; // c
return x * 2685821657736338717ULL;
}
/**
* Returns the current seed in use by the generator.
* @return Current seed.
*/
uint64_t getSeed()
{
return x;
}
/**
* Changes the current seed in use by the generator.
* @param n New seed.
*/
void setSeed(uint64_t n)
{
x = n;
}
/**
* Generates a random integer number within a certain range.
* @param min Minimum number, inclusive.
* @param max Maximum number, inclusive.
* @return Generated number.
*/
int generate(int min, int max)
{
uint64_t num = next();
return (int)(num % (max - min + 1) + min);
}
/**
* Generates a random decimal number within a certain range.
* @param min Minimum number.
* @param max Maximum number.
* @return Generated number.
*/
double generate(double min, double max)
{
double num = next();
return (num / ((double)UINT64_MAX / (max - min)) + min);
}
/**
* Generates a random integer number within a certain range.
* Distinct from "generate" in that it doesn't touch the seed.
* @param min Minimum number, inclusive.
* @param max Maximum number, inclusive.
* @return Generated number.
*/
int seedless(int min, int max)
{
return (rand() % (max - min + 1) + min);
}
/**
* Generates a random percent chance of an event occurring,
* and returns the result
* @param value Value percentage (0-100%)
* @return True if the chance succeeded.
*/
bool percent(int value)
{
return (generate(0, 99) < value);
}
}
}