$urandom_unsigned_value

$urandom generates unsigned values. Consider the following;

  1. int a1;
    a1 = $urandom;
    //generates both positive and negative numbers.

  2. int unsigned a2;
    a2 = $urandom;
    //generates only positive numbers.

  3. int unsigned a3;
    a3 = $random;
    //generates only positive numbers with ‘$random’ keyword and not ‘$urandom’.

So, in order to generate positive numbers, explicit keyword ‘unsigned’ is required. This shows that there is no much difference between $random and $urandom.

In a simpler manner, without declaring the explicit keyword, we can still generate positive numbers;

  1. int a4;
    a4 = $urandom_range(200,0);
    //only positive numbers.

Can we make best practice of method 4) for simiplicity ?

In reply to Mahesh K:

  1. is incorrect. Both $random and $urandom generate 32-bit numbers. As with any assignment, the signed-ness of a value only affects sign-extension when going from a narrow signed value to a wider signed value.

If you only want to deal with positive numbers, then only use unsigned data types.

Also, do not use $random as it provides no stability or global seed control.