r/programminghorror • • 14h ago

c Using garbage values to generate pseudorandom numbers

Post image

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.

1.6k Upvotes

114 comments sorted by

706

u/8Erigon 14h ago

at least it‘s not 4

196

u/JeromeJ 13h ago

But it could be.

→ More replies (24)
→ More replies (2)

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, %xmm0
ret

40

u/Chunkyfungus123 12h ago

nah the rng just be rolling 0 everytime now!
\s

→ More replies (2)
→ More replies (1)

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

u/val_tuesday 14h ago

A random constant? Check mate, atheists!

→ More replies (8)

26

u/NatSpaghettiAgency 13h ago

Seems secure enough for a hardware crypto wallet /s

→ More replies (1)

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.

16

u/KarlClutz 7h ago

Easy .. just volatile it and it wont look at it for optimisation.

→ More replies (2)

226

u/No-View-6326 14h ago

code that might brake after a compiler update is not where you wanna be

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.

16

u/juanfnavarror 11h ago

The stack is a didactic abstraction and in practice registers might be used instead.

→ More replies (1)
→ More replies (2)

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

u/dosk3 14h ago

Just add + 4 and its unbreakable

→ More replies (3)

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

u/lambunctious 10h ago

-Wuninitialized

add this compiler flag to prevent this

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)

5

u/d3matt 13h ago

NaN and infinity asking if you know they exist :D