Files @ r28566:ec28e66fe6ee
Branch filter:

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

dependabot[bot] 49699333+dependabot[bot]@users.noreply.github.com
Upgrade: [CI] bump the actions group with 9 updates (#11881)

Bumps the actions group with 9 updates:

| Package | From | To |
| --- | --- | --- |
| [actions/checkout](https://github.com/actions/checkout) | `3` | `4` |
| [actions/cache](https://github.com/actions/cache) | `3` | `4` |
| [actions/github-script](https://github.com/actions/github-script) | `6` | `7` |
| [OpenTTD/actions](https://github.com/openttd/actions) | `2` | `5` |
| [github/codeql-action](https://github.com/github/codeql-action) | `2` | `3` |
| [actions/download-artifact](https://github.com/actions/download-artifact) | `3` | `4` |
| [actions/upload-artifact](https://github.com/actions/upload-artifact) | `3` | `4` |
| [tibdex/github-app-token](https://github.com/tibdex/github-app-token) | `1` | `2` |
| [peter-evans/repository-dispatch](https://github.com/peter-evans/repository-dispatch) | `2` | `3` |

Updates `actions/checkout` from 3 to 4
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v3...v4)

Updates `actions/cache` from 3 to 4
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v3...v4)

Updates `actions/github-script` from 6 to 7
- [Release notes](https://github.com/actions/github-script/releases)
- [Commits](https://github.com/actions/github-script/compare/v6...v7)

Updates `OpenTTD/actions` from 2 to 5
- [Release notes](https://github.com/openttd/actions/releases)
- [Commits](https://github.com/openttd/actions/compare/v2...v5)

Updates `github/codeql-action` from 2 to 3
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/v2...v3)

Updates `actions/download-artifact` from 3 to 4
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/v3...v4)

Updates `actions/upload-artifact` from 3 to 4
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v3...v4)

Updates `tibdex/github-app-token` from 1 to 2
- [Release notes](https://github.com/tibdex/github-app-token/releases)
- [Commits](https://github.com/tibdex/github-app-token/compare/v1...v2)

Updates `peter-evans/repository-dispatch` from 2 to 3
- [Release notes](https://github.com/peter-evans/repository-dispatch/releases)
- [Commits](https://github.com/peter-evans/repository-dispatch/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: actions/cache
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: actions/github-script
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: OpenTTD/actions
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: github/codeql-action
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: actions/download-artifact
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: actions/upload-artifact
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: tibdex/github-app-token
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
- dependency-name: peter-evans/repository-dispatch
dependency-type: direct:production
update-type: version-update:semver-major
dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r8113:7815c26d7612
r28521:35e6c20016f8
r7935:d51ba3fb01a8
r21895:933c3d6398a1
r21895:933c3d6398a1
r21895:933c3d6398a1
r21895:933c3d6398a1
r21895:933c3d6398a1
r21895:933c3d6398a1
r27148:4e041ae27b9d
r21895:933c3d6398a1
r21895:933c3d6398a1
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28530:9741a9739150
r28530:9741a9739150
r28530:9741a9739150
r28530:9741a9739150
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r8434:4d5d2dc41ce0
r8142:8414f11ec81b
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r27737:728d55b97775
r7935:d51ba3fb01a8
r27737:728d55b97775
r27737:728d55b97775
r7935:d51ba3fb01a8
r28499:cf7f5ff835c8
r28499:cf7f5ff835c8
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r17138:5078c9240593
r20849:fec727013cb8
r20849:fec727013cb8
r20849:fec727013cb8
r20849:fec727013cb8
r17138:5078c9240593
r27737:728d55b97775
r7935:d51ba3fb01a8
r27737:728d55b97775
r8434:4d5d2dc41ce0
r8434:4d5d2dc41ce0
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r27737:728d55b97775
r8434:4d5d2dc41ce0
r8434:4d5d2dc41ce0
r8434:4d5d2dc41ce0
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r17138:5078c9240593
r27737:728d55b97775
r7935:d51ba3fb01a8
r8434:4d5d2dc41ce0
r8434:4d5d2dc41ce0
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r7935:d51ba3fb01a8
r27737:728d55b97775
r7935:d51ba3fb01a8
r16596:a1f46a579763
r28549:6bc33bc6ee38
r8434:4d5d2dc41ce0
r8434:4d5d2dc41ce0
r8477:5efae99a3878
r8434:4d5d2dc41ce0
r7935:d51ba3fb01a8
r27737:728d55b97775
r7935:d51ba3fb01a8
r27737:728d55b97775
r7935:d51ba3fb01a8
r8792:db826506a415
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
r28521:35e6c20016f8
/*
 * 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"
#include "../debug.h"

#ifdef RANDOM_DEBUG
#include "../network/network.h"
#include "../network/network_server.h"
#include "../network/network_internal.h"
#include "../company_func.h"
#include "../fileio_func.h"
#include "../timer/timer_game_calendar.h"
#endif /* RANDOM_DEBUG */

#if defined(_WIN32)
#	include <windows.h>
#	include <bcrypt.h>
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
// No includes required.
#elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
#	include <sys/random.h>
#elif defined(__EMSCRIPTEN__)
#	include <emscripten.h>
#endif

#include "../safeguards.h"

Randomizer _random, _interactive_random;

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

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

/**
 * Generate the next pseudo random number scaled to \a limit, excluding \a limit
 * itself.
 * @param limit Limit of the range to be generated from.
 * @return Random number in [0,\a limit)
 */
uint32_t Randomizer::Next(uint32_t limit)
{
	return ((uint64_t)this->Next() * (uint64_t)limit) >> 32;
}

/**
 * (Re)set the state of the random number generator.
 * @param seed the new state
 */
void Randomizer::SetSeed(uint32_t 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_t seed)
{
	_random.SetSeed(seed);
	_interactive_random.SetSeed(seed * 0x1234567);
}

#ifdef RANDOM_DEBUG
uint32_t DoRandom(int line, const char *file)
{
	if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
		Debug(random, 0, "{:08x}; {:02x}; {:04x}; {:02x}; {}:{}", TimerGameEconomy::date, TimerGameEconomy::date_fract, _frame_counter, (byte)_current_company, file, line);
	}

	return _random.Next();
}

uint32_t DoRandomRange(uint32_t limit, int line, const char *file)
{
	return ((uint64_t)DoRandom(line, file) * (uint64_t)limit) >> 32;
}
#endif /* RANDOM_DEBUG */

/**
 * Fill the given buffer with random bytes.
 *
 * This function will attempt to use a cryptographically-strong random
 * generator, but will fall back to a weaker random generator if none is
 * available.
 *
 * In the end, the buffer will always be filled with some form of random
 * bytes when this function returns.
 *
 * @param buf The buffer to fill with random bytes.
 */
void RandomBytesWithFallback(std::span<uint8_t> buf)
{
#if defined(_WIN32)
	auto res = BCryptGenRandom(nullptr, static_cast<PUCHAR>(buf.data()), static_cast<ULONG>(buf.size()), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
	if (res >= 0) return;
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
	arc4random_buf(buf.data(), buf.size());
	return;
#elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
	auto res = getrandom(buf.data(), buf.size(), 0);
	if (res > 0 && static_cast<size_t>(res) == buf.size()) return;
#elif defined(__EMSCRIPTEN__)
	auto res = EM_ASM_INT({
		var buf = $0;
		var bytes = $1;

		var crypto = window.crypto;
		if (crypto === undefined || crypto.getRandomValues === undefined) {
			return -1;
		}

		crypto.getRandomValues(Module.HEAPU8.subarray(buf, buf + bytes));
		return 1;
	}, buf.data(), buf.size());
	if (res > 0) return;
#else
#	warning "No cryptographically-strong random generator available; using a fallback instead"
#endif

	static bool warned_once = false;
	Debug(misc, warned_once ? 1 : 0, "Cryptographically-strong random generator unavailable; using fallback");
	warned_once = true;

	for (uint i = 0; i < buf.size(); i++) {
		buf[i] = static_cast<uint8_t>(InteractiveRandom());
	}
}