# System Verilog: Random Number System Functions System Verilog provides system functions – \$urandom(),\$urandom_range() and \$srandom() for generating random numbers.

The \$random verilog system function has only one random number generator shared between all threads, but each thread in simulation has its own random number generator for \$urandom and \$urandom_range. Separate random number generators for each thread helps to improve random stability

\$urandom()
The \$urandom() system function provides a mechanism for generating pseudo-random numbers. The function returns a new unsigned 32-bit random number each time it is called.

`function int unsigned \$urandom [ (int seed ) ] ; `

The seed is an optional argument that determines the sequence of random numbers generated.
This generates 32 bit unsigned number, but 2 bits, 4 bits etc. random numbers can be generated as shown below.

```class rand_sysfn_class;

rand bit [1:0] selb;
rand bit [3:0] msel;

function new();
for(int i =0; i<5;i++) begin

selb = \$urandom & 7;  //2 bit random number
\$display ("Selb = %b ",selb);

msel = \$urandom & 15; //4 bit random number
\$display ("msel = %b ",msel);

end
endfunction
endclass

program p1;
rand_sysfn_class c1 = new();
endprogram

/*
///////////////////
Selb = 00
msel = 1011
Selb = 01
msel = 1111
Selb = 01
msel = 0000
Selb = 01
msel = 1100
Selb = 10
msel = 1100

*/
```

\$urandom_range()
The \$urandom_range() function returns an unsigned 32 integer within a specified range. Range would be specified using max and min values as shown below

`function int unsigned \$urandom_range( int unsigned maxval,int unsigned minval = 0 );`

In the below code addr1 range is from 32'hFFFF_FF00 to 32'hFFFF_FFFE. So it generated following values in 5 iterations.

` Addr1 = ffffff24 -> Addr1 = ffffffeb -> Addr1 = ffffff59 -> Addr1 = ffffff4c -> Addr1 = ffffffbf `
```class rand_range_sysfn_class;

function new();
for(int i =0; i<5;i++) begin

end
endfunction
endclass
///////////////////
program p2;
rand_range_sysfn_class c2 = new();
endprogram
/*
*/
```

srandom()
The srandom() method allows manually seeding the RNG of objects or threads.

`function void srandom( int seed );`

The srandom() method initializes an object’s RNG using the value of the given seed.

In the below code 5 iterations are done without setting a seed using \$srandom(). So different random numbers are generated. But once we set srandom(1), RNG will generate same random numbers. This will change once we change the seed.

```
class srand_sysfn_class;

function new();
for(int i =0; i<5;i++) begin
end

for(int i =0; i<5;i++) begin
\$srandom(1);
end
endfunction
endclass

program p1;
srand_sysfn_class c3 = new();
endprogram

/*
Without setting a seed using \$srandom()

Setting a seed using \$srandom()
addr - with srandom = 80010e00
addr - with srandom = 80010e00
addr - with srandom = 80010e00
addr - with srandom = 80010e00
addr - with srandom = 80010e00
*/
``` Sini has spent more than a dozen years in the semiconductor industry, focusing mostly on verification. She is an expert on Formal Verification and has written international papers and articles on related topics.

## 4 comments on “System Verilog: Random Number System Functions”

1. ABHILASH

i executed the program using \$srandom(1). but i didn’t get same random numbers using \$srandom. will you check once the given code is proper. i am using questasim simulation tool

1. Sini Balakrishnan Post author

I tried out again using incisive tool and it works. Please check with Questasim support team on this.

2. PARTH GANDHI

Hey Srini,

what is the difference between \$srandom and \$urandom:
1. as \$urandom (i) produce same results as \$srandom = 1
2. Further, as far as I have read, for each individual seed, numbers should be different i.e. Seed 1 – 5 unique (not identical) values should be different from seed 2 – 5 unique values but by using \$srandom = 1 or \$urandom(i) it gives all 5 values of each seed set as exact same values.
Can you please explain what is the scenario here?

class srand_sysfn_class;

function new();
for(int i =1; i<5;i++) begin
for (int j =0;j<5;j++) begin
end
\$display("*****************");
end

`default_nettype none;

for(int j = 1;j<5 ;j++) begin
int seed = j;
\$display(" seed is set %0d",seed);
void'(\$urandom(seed));
for(int i = 0;i < 5; i++) begin
end
\$display("—————");
end

endfunction
endclass

program p1;
srand_sysfn_class c3 = new();
endprogram

Output:

*****************
*****************
*****************
*****************
seed is set 1
—————
seed is set 2
—————
seed is set 3
3. kishore