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

$random Positive Range

Verilog Random

Use $random to get only positive range of values

Open in IDE
testbench.v
module tb;
  reg [3:0] val;

  initial begin
    for (integer i = 0; i < 10; i += 1) begin
      val = {$random} % 20;
      $display ("val=0x%0h val=%0d", val, val);
    end
  end

endmodule
design.v
// Enter RTL Code
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.