SystemVerilog provides two randomization keywords:
rand - Standard Random
Each value has equal probability on every call. Same value CAN appear
multiple times in a row.
class packet;
rand bit [1:0] priority; // Values: 0, 1, 2, 3
endclass
// Possible sequence over 8 randomizations:
// 2, 0, 0, 3, 1, 0, 2, 0
// Notice: 0 appears 4 times! That's okay for rand.
randc - Cyclic Random
Cycles through all possible values before repeating any value.
Like picking cards from a deck - you see all cards before reshuffling.
class packet;
randc bit [1:0] priority; // Values: 0, 1, 2, 3
endclass
// Possible sequence over 8 randomizations:
// 2, 0, 3, 1 | 1, 3, 0, 2
// Cycle 1 | Cycle 2
// All 4 values appear before any repeats!
When to Use Which?
- rand: When you want truly random distribution (addresses, data)
- randc: When you need to cover all values quickly (opcodes, states,
small enums)
class ahb_transaction;
randc bit [2:0] hburst; // randc: Hit all 8 burst types quickly
randc bit [2:0] hsize; // randc: Test all transfer sizes
rand bit [31:0] haddr; // rand: Address can be any value
rand bit [31:0] hwdata; // rand: Data can be any value
endclass
// After 8 transactions: All hburst values tested!
// After 8 transactions: All hsize values tested!
// haddr and hwdata: Random each time