Weighted Randomisation: randcase and $urandom_range

Constrained-random verification needs more than uniform randomness — you want weighted choices so realistic traffic (mostly reads, occasional errors) emerges. SystemVerilog gives you randcase for weighted branch selection and $urandom_range for bounded values, usable anywhere without a full class constraint.

Weighted selection with randcase

Each randcase item has a weight; the probability of a branch is its weight divided by the total. The example generates 10000 transactions that are 70% read, 25% write, 5% error, and prints the measured distribution — which converges on the weights:

module tb;
  int reads=0, writes=0, errs=0;
  initial begin
    for (int i = 0; i < 10000; i++) begin
      randcase
        70: reads++;     // weight 70
        25: writes++;    // weight 25
         5: errs++;      // weight 5
      endcase
    end
    $display("reads=%0d (~70%%)  writes=%0d (~25%%)  errs=%0d (~5%%)",
             reads, writes, errs);
    $display("random value in [10:20] = %0d", $urandom_range(10,20));
    $finish;
  end
endmodule
reads=7034 (~70%)  writes=2440 (~25%)  errs=526 (~5%)
random value in [10:20] = 16
- /var/folders/l0/gs5hnkm56g54bmxg32r6nnwr0000gn/T/tmpwq345j63/tb.sv:14: Verilog $finish

Check yourself

How is each branch’s probability determined in a <code>randcase</code>?

By its weight divided by the sum of all weights in that randcase. Weights need not sum to 100; a branch with weight 25 out of a total of 100 is selected about 25% of the time.

When would you reach for <code>randcase</code>/<code>$urandom_range</code> instead of a class with <code>rand</code> variables and constraints?

For lightweight, local, weighted stimulus or control-flow decisions that don’t need a full constraint-solved transaction object — e.g. choosing an operation type inside a sequence, or a quick bounded delay. Full constrained-random objects are better when many related fields must satisfy relationships together.

Have questions about this topic?
Share your question in comments or talk to our mentor team for batch guidance.
▶ Try it in the ChipXpert Lab

Compile and simulate this in the browser — no install. Edit the code and re-run instantly.

Open in Verification Lab

Learn Verilog with mentors, labs and placement support

This free series is maintained by the ChipXpert training team in Hyderabad & Bengaluru. Go deeper with instructor-led courses on real EDA tools.

Explore VLSI Courses
Next batch starts 18th November 2026
Start your VLSI career with ChipXpert

Live online and classroom batches in Hyderabad & Bengaluru. Fill in your details and a counsellor will call you back.

  • Real EDA tools in your browser: industry-standard EDAReal EDA tools in your browser
  • Recorded sessions on the elearn portalRecorded sessions
  • Placement assistance: resume, mock interviewsPlacement assistance
  • Merit scholarship up to 60% · EMI optionsScholarship up to 60%
Hi! Ask me about courses, fees, batches or discounts. ×
BENGALURU
HYDERABAD