See section 18.11 Inline random variable control of the 1800-2012 LRM. randomize() can only assign values to variable considered random. Active constraints must be met regardless of whether the variables are random for the call to succeed. So Ex4 will fail if b is not already set to 1 when the call to randomize() is made.
A rarely used feature of randomize() is that you can specify the set of random variables inline with the call as arguments.
// Ex6 a = random, b = 1
assert(f.randomize(a,b) with { b == 1; });
I say this is rarely used because once you list a variable in the argument list of randomize(), you have to list all the random variables, regardless if they were declared rand or not as I did with variable a in the example above.
It is so much simpler to set the non-random class member to the value you want prior to calling randomize. Or you could do it afterwards if its value has no effect on the existing constraints.
Avoid using the “assert” in the randomize because a $assertoff will stop your randomization. $assertoff stops the checking of all specified assertions until a subsequent $asserton . An assertion that is already executing, including execution of the pass or fail statement, is not affected.
Instead, use something like the following:
if(!f.randomize() with { a == 0 }) `uvm_error("MYERR", "This is a randomize error");