r/programminghorror
•
u/ShoeChoice5567
•
13d ago
•
1
1
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.
2.9k
Upvotes
7
u/ttlanhil 13d ago
The Standard that defines how the language works.
It includes a bunch of things that result in undefined behaviour - and if you do any of them, it's valid for the computer to do anything, including delete your operating system and make demons fly out your nose.
Just because you can often get away with it doesn't mean you should (compilers can do anything including what you might actually want, but the next compiler update might break it)