Files @ r18783:bc122a900899
Branch filter:

Location: cpp/openttd-patchpack/source/src/core/random_func.cpp

translators
(svn r23641) -Update from WebTranslator v3.0:
afrikaans - 98 changes by Maccie123, kdzar
bulgarian - 4 changes by Tvel
catalan - 44 changes by arnau
simplified_chinese - 62 changes by ww9980
traditional_chinese - 46 changes by elleryq, ww9980
croatian - 43 changes by VoyagerOne
danish - 188 changes by majbom, mgarde
dutch - 44 changes by habell
english_US - 32 changes by Rubidium
finnish - 43 changes by jpx_
french - 43 changes by glx
frisian - 16 changes by Fopper
german - 44 changes by NG, planetmaker
greek - 49 changes by kyrm
indonesian - 31 changes by fanioz
korean - 39 changes by junho2813, telk5093
lithuanian - 66 changes by BlinK_, Devastator
malay - 36 changes by Syed
maltese - 20 changes by ercolesptr
norwegian_bokmal - 6 changes by kristoffer_hh
norwegian_nynorsk - 1 changes by Utvik
polish - 68 changes by lion, matma6, nouwak, wojteks86
portuguese - 104 changes by ABCRic
romanian - 148 changes by kneekoo, tonny
russian - 43 changes by Lone_Wolf, MajestiC, akasoft
slovak - 135 changes by teso
spanish - 43 changes by Terkhen
swedish - 38 changes by Zuu, urdh
ukrainian - 28 changes by Madvin
vietnamese - 3 changes by nglekhoi
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file random_func.cpp Implementation of the pseudo random generator. */

#include "../stdafx.h"
#include "random_func.hpp"
#include "bitmath_func.hpp"

Randomizer _random, _interactive_random;

/**
 * Generate the next pseudo random number
 * @return the random number
 */
uint32 Randomizer::Next()
{
	const uint32 s = this->state[0];
	const uint32 t = this->state[1];

	this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
	return this->state[1] = ROR(s, 3) - 1;
}

/**
 * Generate the next pseudo random number scaled to max
 * @param max the maximum value of the returned random number
 * @return the random number
 */
uint32 Randomizer::Next(uint32 max)
{
	return ((uint64)this->Next() * (uint64)max) >> 32;
}

/**
 * (Re)set the state of the random number generator.
 * @param seed the new state
 */
void Randomizer::SetSeed(uint32 seed)
{
	this->state[0] = seed;
	this->state[1] = seed;
}

/**
 * (Re)set the state of the random number generators.
 * @param seed the new state
 */
void SetRandomSeed(uint32 seed)
{
	_random.SetSeed(seed);
	_interactive_random.SetSeed(seed * 0x1234567);
}

#ifdef RANDOM_DEBUG
#include "../network/network.h"
#include "../network/network_server.h"
#include "../network/network_internal.h"
#include "../company_func.h"

uint32 DoRandom(int line, const char *file)
{
	if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
		printf("Random [%d/%d] %s:%d\n", _frame_counter, (byte)_current_company, file, line);
	}

	return _random.Next();
}

uint32 DoRandomRange(uint32 max, int line, const char *file)
{
	return ((uint64)DoRandom(line, file) * (uint64)max) >> 32;
}
#endif /* RANDOM_DEBUG */