Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

pre_randomize & post_randomize: Calling randomize()

SystemVerilog Randomization

Runnable example from the SystemVerilog pre_randomize & post_randomize tutorial (Calling randomize())

Open in IDE
testbench.sv
class Beverage;
  rand bit [7:0]	beer_id;
  
  constraint c_beer_id { beer_id >= 10;
                        beer_id <= 50; };
  
endclass

module tb;
   Beverage b;
  
    initial begin
      b = new ();
      $display ("Initial beerId = %0d", b.beer_id);
      if (b.randomize ())
      	$display ("Randomization successful !");
      $display ("After randomization beerId = %0d", b.beer_id);
    end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.