Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / core / random_func.cpp
blob8cf21b54f5c19ba0c6c7041ec17408c199566151
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file random_func.cpp Implementation of the pseudo random generator. */
12 #include "../stdafx.h"
13 #include "random_func.hpp"
14 #include "bitmath_func.hpp"
16 Randomizer _random, _interactive_random;
18 /**
19 * Generate the next pseudo random number
20 * @return the random number
22 uint32 Randomizer::Next()
24 const uint32 s = this->state[0];
25 const uint32 t = this->state[1];
27 this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
28 return this->state[1] = ROR(s, 3) - 1;
31 /**
32 * Generate the next pseudo random number scaled to \a limit, excluding \a limit
33 * itself.
34 * @param limit Limit of the range to be generated from.
35 * @return Random number in [0,\a limit)
37 uint32 Randomizer::Next(uint32 limit)
39 return ((uint64)this->Next() * (uint64)limit) >> 32;
42 /**
43 * (Re)set the state of the random number generator.
44 * @param seed the new state
46 void Randomizer::SetSeed(uint32 seed)
48 this->state[0] = seed;
49 this->state[1] = seed;
52 /**
53 * (Re)set the state of the random number generators.
54 * @param seed the new state
56 void SetRandomSeed(uint32 seed)
58 _random.SetSeed(seed);
59 _interactive_random.SetSeed(seed * 0x1234567);
62 #ifdef RANDOM_DEBUG
63 #include "../network/network.h"
64 #include "../network/network_server.h"
65 #include "../network/network_internal.h"
66 #include "../company_func.h"
67 #include "../fileio_func.h"
68 #include "../date_func.h"
70 uint32 DoRandom(int line, const char *file)
72 if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
73 DEBUG(random, 0, "%08x; %02x; %04x; %02x; %s:%d", _date, _date_fract, _frame_counter, (byte)_current_company, file, line);
76 return _random.Next();
79 uint32 DoRandomRange(uint32 limit, int line, const char *file)
81 return ((uint64)DoRandom(line, file) * (uint64)limit) >> 32;
83 #endif /* RANDOM_DEBUG */