DescriptionQ321
Q321DVAppleASIC interview problem
Weighted random values without dist
TechniquesDVSystemVerilogRandomizationProbability
DifficultyEasy
TopicConstrained Random
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
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.
Function headerSystemVerilog
function automatic int weighted_value();
// Return 1 with probability 1/3 and 2 with probability 2/3.
endfunctionExample input and output
Use this case to check your interpretationInput
uniform raw outcomes: 0, 1, 2Output
mapped values: 1, 2, 2Explanation
One of three equally likely outcomes returns 1, while two return 2, giving exact probabilities 1/3 and 2/3.
02
Requirements (4)
- 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.
