# Why randomize () is called inside the initial block which is in a module?

**URL:** <https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-enum-classes-constraints\
**Created:** [October 14, 2021, 9:01pm UTC](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701 "2021-10-14T21:01:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sk7799](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sk7799](https://verificationacademy.com/forums/u/sk7799)\
**Post date:** [October 14, 2021, 9:01pm UTC](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/1 "2021-10-14T21:01:42Z")

</div>

Hi,

I understand that class is dynamic and module is static.  
In the below example I see that the rand variables are declared in the class. But the randomize() function is being called in the initial block of the module. In some other examples, I see randomize() being called in the initial block that sits inside the class.

I am confused as in what is the correct way of calling the randomize() method and why?  
I tried searching this online but I am not convinced with the answers.

Any help would be much appreciated.

Sample example:

```verilog
class packet;
  rand byte addr;
  rand byte data;  
endclass
 
module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
     
    //calling randomize method
    pkt.randomize();
     
    $display("\taddr = %0d \t data = %0d",pkt.addr,pkt.data);
  end
endmodule

```

Thank you.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 14, 2021, 11:07pm UTC](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/2 "2021-10-14T23:07:49Z")

</div>

_In reply to [sk7799](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701):_

There are no **initial** blocks inside classes. Procedural processes are started with initial/always procedures inside a module or interface. You can have a call to randomize() inside a class method, but that class method has to called from an initial/always procedure, or another method/procedure. The point is something _static_ has to be at the root of a call chain.

---

<div class="post-metadata">

**Author:** ![sk7799](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sk7799](https://verificationacademy.com/forums/u/sk7799)\
**Post date:** [October 14, 2021, 11:22pm UTC](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/3 "2021-10-14T23:22:37Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/2):_

Hi Dave,

Thank you for your response.  
It’s much clear for me now.

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [October 14, 2021, 11:46pm UTC](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/4 "2021-10-14T23:46:56Z")

</div>

_In reply to [sk7799](https://verificationacademy.com/forums/t/why-randomize-is-called-inside-the-initial-block-which-is-in-a-module/39701/3):_

> what is the correct way of calling the randomize() method and why?

That depends on your verification methodology and your goals. Consider the following possibilities:

- **UVM methodology** Here your randomize will be in the sequence

```verilog

class rand_seq extends uvm_sequence #( counter_xactn, counter_xactn );
...
task body();
req = counter_xactn::type_id::create("req");
forever begin
start_item(req);
// The contents of the sequence item are populated between the calls to
// start_item() and finish_item(), either through directed or randomized means.
// assert(req.randomize()); // ** AVIOD THIS
if (!req.randomize()) `uvm_error("MYERR", "This is a randomize error");
//`uvm_info({get_type_name(),":body"},{"Sending transaction ",req.convert2string()}, OVM_MEDIUM)
finish_item(req);
//`uvm_info("rand_seq", "finished_item in task body ovm_low", OVM_LOW)
end
endtask: body
endclass: rand_seq 

```

- **A la UVM, but not UVM** Here the randomize is in a class, see  
[Systemverilog or UVM? - SystemVerilog - Verification Academy](https://verificationacademy.com/forums/systemverilog/systemverilog-or-uvm#reply-102209)

```verilog

class simple_random_sequence;
transaction tx=new();
virtual task run();
if (!randomize(tx)) `uvm_error("MYERR", "This is a randomize error");
// if(!randomize(tx)) $error("randomization failure");
endtask : run
endclass : simple_random_sequence
module tb;
...
// test using simple_random_sequence
always @ (posedge clk) begin : random1
..
for (int i=0; i<40; i++) begin : for1
simple_rand_sequence_ld_mostly_hi.run();
data_in <= simple_rand_sequence_ld_mostly_hi.tx.data;
ld <= simple_rand_sequence_ld_mostly_hi.tx.ld;
@ (posedge clk);
end : for1
$finish;
end : random1 

```

- **Quick testbench** I randomize in the initial statement. I do that to test my assertions

```verilog

module top;
timeunit 1ns; timeprecision 100ps;
`include "uvm_macros.svh" import uvm_pkg::*;
bit clk, a, b, reset_n;
initial forever #10 clk = !clk;
...
// my assertions her
initial begin
bit v_a, v_b, v_err;
repeat (200) begin
@(posedge clk);
if (!randomize(v_a, v_b, v_err) with {
v_a dist {1'b1 := 1, 1'b0 := 1};
v_b dist {1'b1 := 1, 1'b0 := 2};
v_err dist {1'b1 := 1, 1'b0 := 15};
}) `uvm_error("MYERR", "This is a randomize error");
a <= v_a;
if(v_err==0) b<=v_b; else b<=!v_b;
end
$finish;
end
endmodule

```

The point, the **if (!randomize …** can be anywhere…  
// (edited) anywhere inside a procedural context.  
Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)  
\*\* SVA Handbook 4th Edition, 2016 ISBN 978-1518681448  
…

1. SVA Package: Dynamic and range delays and repeats [SVA: Package for dynamic and range delays and repeats - SystemVerilog - Verification Academy](https://rb.gy/a89jlh)
2. Free books: Component Design by Example [https://rb.gy/9tcbhl](https://rb.gy/9tcbhl)  
Real Chip Design and Verification Using Verilog and VHDL($3) [https://rb.gy/cwy7nb](https://rb.gy/cwy7nb)
3. Papers:

- Understanding the SVA Engine,  
[https://verificationacademy.com/verification-horizons/july-2020-volume-16-issue-2](https://verificationacademy.com/verification-horizons/july-2020-volume-16-issue-2)
- SVA Alternative for Complex Assertions  
[https://verificationacademy.com/news/verification-horizons-march-2018-issue](https://verificationacademy.com/news/verification-horizons-march-2018-issue)
- SVA in a UVM Class-based Environment  
[https://verificationacademy.com/verification-horizons/february-2013-volume-9-issue-1/SVA-in-a-UVM-Class-based-Environment](https://verificationacademy.com/verification-horizons/february-2013-volume-9-issue-1/SVA-in-a-UVM-Class-based-Environment)

Udemy courses by Srinivasan Venkataramanan ([http://cvcblr.com/home.html](http://cvcblr.com/home.html))  
[https://www.udemy.com/course/sva-basic/](https://www.udemy.com/course/sva-basic/)  
[https://www.udemy.com/course/sv-pre-uvm/](https://www.udemy.com/course/sv-pre-uvm/)
