Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Weighted random values without dist

Hardware interview practice

Weighted random values without dist

EasyConstrained RandomSystemVerilog

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.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

function automatic int weighted_value();
  // Return 1 with probability 1/3 and 2 with probability 2/3.
endfunction
Reviewed example

Work through one case

Input
uniform raw outcomes: 0, 1, 2
Expected output
mapped values: 1, 2, 2

One of three equally likely outcomes returns 1, while two return 2, giving exact probabilities 1/3 and 2/3.

What to cover

Requirements

  1. Call $urandom_range(2, 0) exactly once for each returned value.
  2. Map raw outcome 0 to 1 and raw outcomes 1 and 2 to 2.
  3. Return no value other than 1 or 2.
  4. Explain the exact probability from the three equiprobable raw outcomes.
Continue practicing

Related questions

ConstraintsPick a weighted random enum→ConstraintsBias burst lengths by range→
asic.fyi · Learn silicon end to end.info@asic.fyi