Getting Wrong output from $unsigned($random)

module top();
integer address;
initial
begin
repeat (5)
#10 address = $unsigned($random);
end

  initial $monitor("address = %0d", address);
  endmodule

Without using $unsigned it is printing signed addresses and with using $unsigned also it is printing the same signed address

In reply to bhar3777:

The variable ‘address’ is declared as signed. Therefore, anything that is assigned to it will be converted to signed.

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

In reply to MayurKubavat:

You don’t want to set bit 31 to zero as this eliminates 50% of the possible values. If you want a true unsigned variable, then declare it as unsigned.

In reply to cgales:

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 MayurKubavat:

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

Thank You MayurKubavat…

In reply to bhar3777:

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.