That’s right. Also addresses will not be negative, it’s better to use unsigned type.
But still, by some means when negative addresses are also needed and you also want random positive addresses over a period of time, above should work! Eliminate 50% of addresses which are negative.
In reply to bhar3777:
Value returned from $unsigned($random) is unsigned, but when its being assigned to address(type integer), it is converted to signed.
MSB of address has signed weight, keep this bit 0 to get unsigned values.
module top();
integer address;
initial
begin
repeat(5)
//integer is signed 32-bit
#10 address[30:0] = $random;
end
initial $monitor("address = %0d", address);
endmodule
Setting the first bit to 0 is NOT the correct solution as it eliminates 50% of the possible 32 bit values. If you need a 32 bit address that is unsigned, then you need to use an unsigned variable. If you only wanted a 31 bit address, then you should use that declaration instead of an integer.