r/programminghorror • • Oct 26 '26 • Popcorn 1 Bravo 1

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.

2.9k Upvotes

152 comments sorted by

View all comments

Show parent comment

1

u/etiennec78 Oct 30 '26

How ?

1

u/etan1 Oct 30 '26

well, the bit pattern is randomized, and an entire range of them are nans, either signalling or nonsignalling ones

3

u/etiennec78 Oct 30 '26

I don't think that's the case ? This is a cast not a memcpy, so the uint32 value will be converted to a double (64), keeping the same value but not the same binary arrangement.

1

u/etan1 Oct 30 '26

unsigned is not necessarily a uint32 depending on the arch it could be narrower or wider

but you are right. whats being converted is the int value, not the int pattern, so can’t go nan

1

u/etiennec78 Oct 30 '26

You're right about uint32, sorry about that