summaryrefslogtreecommitdiff
path: root/src/com/sneed/pkrandom/RandomSource.java
blob: 97eef30c0730dbf26b8ebae85a75b35b04ef9967 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.sneed.pkrandom;

/*----------------------------------------------------------------------------*/
/*--  RandomSource.java - functions as a centralized source of randomness   --*/
/*--                      to allow the same seed to produce the same random --*/
/*--                      ROM consistently.                                 --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Originally part of "Universal Pokemon Randomizer" by sneed        --*/
/*--  Pokemon and any associated names and the like are                     --*/
/*--  trademark and (C) Nintendo 1996-2020.                                 --*/
/*--                                                                        --*/
/*--  The custom code written here is licensed under the terms of the GPL:  --*/
/*--                                                                        --*/
/*--  This program 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.                                   --*/
/*--                                                                        --*/
/*--  This program 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 this program. If not, see <http://www.gnu.org/licenses/>.  --*/
/*----------------------------------------------------------------------------*/

import java.security.SecureRandom;
import java.util.Random;

public class RandomSource {

    private static Random source = new Random();
    private static Random cosmeticSource = new Random();
    private static int calls = 0;
    private static int cosmeticCalls = 0;
    private static Random instance = new RandomSourceInstance();
    private static Random cosmeticInstance = new CosmeticRandomSourceInstance();

    public static void reset() {
        source = new Random();
        cosmeticSource = new Random();
        calls = 0;
        cosmeticCalls = 0;
    }

    public static void seed(long seed) {
        source.setSeed(seed);
        cosmeticSource.setSeed(seed);
        calls = 0;
        cosmeticCalls = 0;
    }

    public static double random() {
        calls++;
        return source.nextDouble();
    }

    public static int nextInt(int size) {
        calls++;
        return source.nextInt(size);
    }

    public static int nextIntCosmetic(int size) {
        cosmeticCalls++;
        return cosmeticSource.nextInt(size);
    }

    public static void nextBytes(byte[] bytes) {
        calls++;
        source.nextBytes(bytes);
    }

    public static int nextInt() {
        calls++;
        return source.nextInt();
    }

    public static long nextLong() {
        calls++;
        return source.nextLong();
    }

    public static boolean nextBoolean() {
        calls++;
        return source.nextBoolean();
    }

    public static float nextFloat() {
        calls++;
        return source.nextFloat();
    }

    public static double nextDouble() {
        calls++;
        return source.nextDouble();
    }

    public static synchronized double nextGaussian() {
        calls++;
        return source.nextGaussian();
    }

    public static long pickSeed() {
        long value = 0;
        byte[] by = SecureRandom.getSeed(6);
        for (int i = 0; i < by.length; i++) {
            value |= ((long) by[i] & 0xffL) << (8 * i);
        }
        return value;
    }

    public static Random instance() {
        return instance;
    }

    public static Random cosmeticInstance() {
        return cosmeticInstance;
    }

    public static int callsSinceSeed() {
        return calls + cosmeticCalls;
    }

    private static class RandomSourceInstance extends Random {

        /**
         * 
         */
        private static final long serialVersionUID = -4876737183441746322L;

        @Override
        public synchronized void setSeed(long seed) {
            RandomSource.seed(seed);
        }

        @Override
        public void nextBytes(byte[] bytes) {
            RandomSource.nextBytes(bytes);
        }

        @Override
        public int nextInt() {
            return RandomSource.nextInt();
        }

        @Override
        public int nextInt(int n) {
            return RandomSource.nextInt(n);
        }

        @Override
        public long nextLong() {
            return RandomSource.nextLong();
        }

        @Override
        public boolean nextBoolean() {
            return RandomSource.nextBoolean();
        }

        @Override
        public float nextFloat() {
            return RandomSource.nextFloat();
        }

        @Override
        public double nextDouble() {
            return RandomSource.nextDouble();
        }

        @Override
        public synchronized double nextGaussian() {
            return RandomSource.nextGaussian();
        }

    }

    private static class CosmeticRandomSourceInstance extends Random {

        @Override
        public synchronized void setSeed(long seed) {
            RandomSource.seed(seed);
        }

        @Override
        @Deprecated
        public void nextBytes(byte[] bytes) {

        }

        @Override
        @Deprecated
        public int nextInt() {
            return 0;
        }

        @Override
        public int nextInt(int n) {
            return RandomSource.nextIntCosmetic(n);
        }

        @Override
        @Deprecated
        public long nextLong() {
            return 0;
        }

        @Override
        @Deprecated
        public boolean nextBoolean() {
            return false;
        }

        @Override
        @Deprecated
        public float nextFloat() {
            return 0;
        }

        @Override
        @Deprecated
        public double nextDouble() {
            return 0;
        }

        @Override
        @Deprecated
        public synchronized double nextGaussian() {
            return 0;
        }
    }
}