1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23// This file contains portable random functions for SDL
24
25static Uint64 SDL_rand_state;
26static bool SDL_rand_initialized = false;
27
28void SDL_srand(Uint64 seed)
29{
30 if (!seed) {
31 seed = SDL_GetPerformanceCounter();
32 }
33 SDL_rand_state = seed;
34 SDL_rand_initialized = true;
35}
36
37Sint32 SDL_rand(Sint32 n)
38{
39 if (!SDL_rand_initialized) {
40 SDL_srand(0);
41 }
42
43 return SDL_rand_r(&SDL_rand_state, n);
44}
45
46float SDL_randf(void)
47{
48 if (!SDL_rand_initialized) {
49 SDL_srand(0);
50 }
51
52 return SDL_randf_r(&SDL_rand_state);
53}
54
55Uint32 SDL_rand_bits(void)
56{
57 if (!SDL_rand_initialized) {
58 SDL_srand(0);
59 }
60
61 return SDL_rand_bits_r(&SDL_rand_state);
62}
63
64Uint32 SDL_rand_bits_r(Uint64 *state)
65{
66 if (!state) {
67 return 0;
68 }
69
70 // The C and A parameters of this LCG have been chosen based on hundreds
71 // of core-hours of testing with PractRand and TestU01's Crush.
72 // Using a 32-bit A improves performance on 32-bit architectures.
73 // C can be any odd number, but < 256 generates smaller code on ARM32
74 // These values perform as well as a full 64-bit implementation against
75 // Crush and PractRand. Plus, their worst-case performance is better
76 // than common 64-bit constants when tested against PractRand using seeds
77 // with only a single bit set.
78
79 // We tested all 32-bit and 33-bit A with all C < 256 from a v2 of:
80 // Steele GL, Vigna S. Computationally easy, spectrally good multipliers
81 // for congruential pseudorandom number generators.
82 // Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
83 // https://arxiv.org/abs/2001.05304v2
84
85 *state = *state * 0xff1cd035ul + 0x05;
86
87 // Only return top 32 bits because they have a longer period
88 return (Uint32)(*state >> 32);
89}
90
91Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
92{
93 // Algorithm: get 32 bits from SDL_rand_bits() and treat it as a 0.32 bit
94 // fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit
95 // result. Shift right by 32 to get the 31 bit integer that we want.
96
97 if (n < 0) {
98 // The algorithm looks like it works for numbers < 0 but it has an
99 // infinitesimal chance of returning a value out of range.
100 // Returning -SDL_rand(abs(n)) blows up at INT_MIN instead.
101 // It's easier to just say no.
102 return 0;
103 }
104
105 // On 32-bit arch, the compiler will optimize to a single 32-bit multiply
106 Uint64 val = (Uint64)SDL_rand_bits_r(state) * n;
107 return (Sint32)(val >> 32);
108}
109
110float SDL_randf_r(Uint64 *state)
111{
112 // Note: its using 24 bits because float has 23 bits significand + 1 implicit bit
113 return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;
114}
115
116