How is the rand_mode() method used in SystemVerilog to enable or disable…
How is the rand_mode() method used in SystemVerilog to enable or disable randomization of rand / randc variables? Illustrate with a code example.
Model answer
In SystemVerilog, the built-in ** rand_mode() ** method controls whether a rand or randc variable (or all random variables in a class instance) is randomized when randomize() is called: - ** rand_mode(0) **: Disables randomization. The variable behaves as a non-random state variable (retaining its current value while still being checked against active constraints). - ** rand_mode(1) **: Re-enables randomization (the default state for all rand / randc variables). - ** rand_mode() (called as a function with no arguments)**: Returns the current active state ( 1 if randomization is enabled, 0 if disabled) for a specific random variable. **Scope of rand_mode() :** - Calling <obj>.<var>.rand_mode(0) disables randomization for that **single variable**. - Calling <obj>.rand_mode(0) disables randomization for **all** rand / randc variables inside <obj> .