Hardware interview practice
Weighted random values without dist
Write a SystemVerilog function that returns 1 with probability 1/3 and 2 with probability 2/3. Use exactly one $urandom_range call and do not use dist or constraints.
Starting point
Question code
function automatic int weighted_value();
// Return 1 with probability 1/3 and 2 with probability 2/3.
endfunctionReviewed example
Work through one case
Input
uniform raw outcomes: 0, 1, 2Expected output
mapped values: 1, 2, 2One of three equally likely outcomes returns 1, while two return 2, giving exact probabilities 1/3 and 2/3.
What to cover
Requirements
- Call $urandom_range(2, 0) exactly once for each returned value.
- Map raw outcome 0 to 1 and raw outcomes 1 and 2 to 2.
- Return no value other than 1 or 2.
- Explain the exact probability from the three equiprobable raw outcomes.
