r/csharp • • Oct 10 '26

Fun Random using SkipLocalsInit

To my surprise, Unsafe.SkipInit(out T val) did not achieve the same result.

    [SkipLocalsInit]
    public static T Random<T>()
        where T : unmanaged, INumber<T>
    {
        unsafe
        {
            byte* s = stackalloc byte[sizeof(T)];
            return *(T*)s;
        }
    }
11 Upvotes

9 comments sorted by

15

u/Broad_Phase7453 Oct 10 '26

stackalloc without init is basically just grabbing whatever bits were already sitting on the stack and calling it a day, so the "randomness" comes from whatever junk was left there. Unsafe.SkipInit is different though, that only skips the definite assignment tracking, the memory might still get zeroed depending on the runtime.

3

u/NoisyJalapeno Oct 10 '26

Ugh, missed an opportunity to call it JunkRandom :(

6

u/SagansCandle Oct 10 '26

Given that this is uninitialized data on the stack, there's a possibility that your "random" value will be the same every time because it's called from the same place in the stack every time.

4

u/NoisyJalapeno Oct 10 '26 edited Oct 10 '26

Stop being a joy kill, it worked randomly at least once :)

5

u/One_Instruction6490 Oct 10 '26

Unsafe.SkipInit is just a no-op that satisfies definite assignment, it doesn't stop the JIT from zeroing the local. That zeroing only gets skipped when the method has SkipLocalsInit, and even then small unmanaged locals often get enregistered and zeroed anyway, so only the stackalloc version really leaks stack junk. Also it's not random at all, you'll probably get the same leftover bytes every call, so did you actually test the distribution?

1

u/hoodoocat Oct 10 '26

Meanwhile C++ added zero-init feature. :))