Randomization

You didn’t describe how your results differ, but when I run your 2 code samples, I see the following differences. When I run code1, I get the same number 10 times. When I run code2, I get 10 different numbers.

When I run code1 with the Cadence Incisive simulator, I get a warning that I should declare i with the static keyword. I believe the difference in behavior is related to variable lifetime, as described in the IEEE Std 1800-2012, section “6.21 Scope and lifetime”.

If I change code1 and declare i using the automatic keyword, I get 10 different numbers:

automatic int i = $urandom() ;