/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// This file contains portable random functions for SDL2324static Uint64 SDL_rand_state;25static bool SDL_rand_initialized = false;2627void SDL_srand(Uint64 seed)28{29if (!seed) {30seed = SDL_GetPerformanceCounter();31}32SDL_rand_state = seed;33SDL_rand_initialized = true;34}3536Sint32 SDL_rand(Sint32 n)37{38if (!SDL_rand_initialized) {39SDL_srand(0);40}4142return SDL_rand_r(&SDL_rand_state, n);43}4445float SDL_randf(void)46{47if (!SDL_rand_initialized) {48SDL_srand(0);49}5051return SDL_randf_r(&SDL_rand_state);52}5354Uint32 SDL_rand_bits(void)55{56if (!SDL_rand_initialized) {57SDL_srand(0);58}5960return SDL_rand_bits_r(&SDL_rand_state);61}6263Uint32 SDL_rand_bits_r(Uint64 *state)64{65if (!state) {66return 0;67}6869// The C and A parameters of this LCG have been chosen based on hundreds70// of core-hours of testing with PractRand and TestU01's Crush.71// Using a 32-bit A improves performance on 32-bit architectures.72// C can be any odd number, but < 256 generates smaller code on ARM3273// These values perform as well as a full 64-bit implementation against74// Crush and PractRand. Plus, their worst-case performance is better75// than common 64-bit constants when tested against PractRand using seeds76// with only a single bit set.7778// We tested all 32-bit and 33-bit A with all C < 256 from a v2 of:79// Steele GL, Vigna S. Computationally easy, spectrally good multipliers80// for congruential pseudorandom number generators.81// Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.303082// https://arxiv.org/abs/2001.05304v28384*state = *state * 0xff1cd035ul + 0x05;8586// Only return top 32 bits because they have a longer period87return (Uint32)(*state >> 32);88}8990Sint32 SDL_rand_r(Uint64 *state, Sint32 n)91{92// Algorithm: get 32 bits from SDL_rand_bits() and treat it as a 0.32 bit93// fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit94// result. Shift right by 32 to get the 31 bit integer that we want.9596if (n < 0) {97// The algorithm looks like it works for numbers < 0 but it has an98// infinitesimal chance of returning a value out of range.99// Returning -SDL_rand(abs(n)) blows up at INT_MIN instead.100// It's easier to just say no.101return 0;102}103104// On 32-bit arch, the compiler will optimize to a single 32-bit multiply105Uint64 val = (Uint64)SDL_rand_bits_r(state) * n;106return (Sint32)(val >> 32);107}108109float SDL_randf_r(Uint64 *state)110{111// Note: its using 24 bits because float has 23 bits significand + 1 implicit bit112return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;113}114115116117