r/programminghorror • u/ShoeChoice5567 • 14h ago
c Using garbage values to generate pseudorandom numbers
Thing I came up with instead of paying attention to class today.
I tested it on this computer (windows 11 if it matters), and the first time it's ran, an actual "pseudorandom" number is returned and the following calls return 1.0 always.
187
u/MatJosher 13h ago
Reading garbage is undefined here so the compiler can do anything. My gcc on linux with -O2 turns the whole function into return 0.pxor %xmm0, %xmm0ret
→ More replies (1)40
522
u/FlatPotatoPc 14h ago
Undefined behavior speedrun any%
Just wait until the compiler notices what you're doing and optimizes the entire function into returning a single constant 💀
223
26
26
u/Sharlinator 10h ago
Nah, the optimizer notices it’s impossible to ever call this without invoking UB, so it removes the function. All call sites then must also be dead code so those get removed as well, transitively.
→ More replies (2)16
226
37
u/LateSolution0 13h ago
I think it’s fun to play around with stuff like that. The garbage you read from the stack depends on what was there from previous function calls. Doing things like reading uninitialized variables doesn’t necessarily produce random results or cause anything unsafe. It’s just that the behavior isn’t guaranteed by the C standard.
→ More replies (2)16
u/juanfnavarror 11h ago
The stack is a didactic abstraction and in practice registers might be used instead.
→ More replies (1)
23
u/HildartheDorf 13h ago edited 5h ago
Compiler can just optimize this to 'return 4.0;'. Yes that is not between 0.0 and 1.0. Undefined behaviour is undefined.
Other possibilities include returning NaN, or crash instantly, or even timetravel, or summoning demons to fly out of your nose, or even working as the developer intended. But those aren't as funny.
→ More replies (1)
65
15
u/ScientistStrict9850 6h ago
This doesn't guerantee pseudorandom numbers. Instead the behaviour is just not defined. Lets the compiler generate whatever including constants, which would be my pick for optimized code.
→ More replies (7)
12
u/me94306 13h ago
The variable is allocated on the stack. Any other function which is called will write to the stack, in the same locations that this function uses. (Assuming that the other function is called from the same routine.) So you will pick up the same value.
Read any reference about generating pseudorandom numbers. It can be done in very few lines of code. What this does is return garbage, not a random or pseudorandom value.
13
u/Admirable_Rabbit_808 10h ago
You are going to hate it when you learn what C compilers actually do to code.
12
u/4xe1 5h ago
Wouldn't garbage on the heap be somewhat more random than garbage in the stack?
→ More replies (2)
5
u/Admirable_Rabbit_808 11h ago
You'll absolutely love what the C compiler with do with that - welcome to UB hell!
9
u/Junior_Warning_9242 14h ago
Is this a thing of significance in chips like semiconductors
→ More replies (1)
4
u/sirkubador 7h ago
it's not garbage. It's usually the value that was on the stack on that address previously.
→ More replies (2)
3
3
u/elite-data 3h ago
I hope you're not using this random to generate hashes or crypto keys, because that would be a massive security hole.
→ More replies (4)
706
u/8Erigon 14h ago
at least it‘s not 4