Issue in getting expected randomized value

I have this snippet of code to get result of c=1 on the basis when a=10;

If i have 2nd constraint then my randomization is not working as expected
program main;
class ex;
rand int unsigned a,b;
rand bit c;
constraint abassign_c { a==10; b==10;}//if i have This constraint alone iam getting a=10 and b=10
constraint ab_c { a==10 → c==1;}
endclass
initial begin
ex ex_i;
ex_i=new();
void’(ex_i.randomize());
$display(“Transaction is %p”,ex_i);
end
endprogram

Output us below:

VSIM 1> run

Transaction is '{a:0, b:0, c:0}

End time: 10:53:34 on May 18,2015, Elapsed time: 0:00:39

Errors: 0, Warnings: 0

Try this:

program main;

class ex;
  rand int unsigned a,b;
  rand bit c; // Must be a random variable to be constrained

  constraint abassign_c { a==10; b==10;}
  constraint ab_c { a==10 -> c==1;} 
  
  constraint a_before_c {
    solve a before c;
  };  
endclass

initial begin
  ex ex_i =new();

  if(!ex_i.randomize()) $display("Radomization failed"); 
  // Will fail if variable 'c' is not of rand type
  else $display("Transaction is %p",ex_i);
end 

endprogram

Result:

run -all;
# KERNEL: Transaction is '{a:10, b:10, c:1}
# RUNTIME: Info: RUNTIME_0068 testbench.sv (20): $finish called.

declare bit c as random , then you will not get the randomization failure.
if you dont mention bit c as rand ,when you randomize the object the value assigned to c is 0, and the constraint constrains bit c to 1, which is conflict.

In reply to bharath123:

You should always test the result of a call to randomize().

And in the future when posting a question, it helps to say more than just it is “not working”.

In reply to Sushrut Veerapur:

Thanks.

Can we have nested if inside constraint?

If i have that it is not giving any compilation issue but iam not getting the expected output.

In reply to bharath123:

Please publish your complete code.

In reply to Sushrut Veerapur:

As per my requirement i was required to write constraint as below,which is working and generated as per my requirement

constraint cons1_c{if(days==SUN) {dress_code==BLUE;boys.size==2; girls.size==2;} else if(days==MON && group==G1) { dress_code==WHITE; boys.size==1; } else if(days==MON && group==G2) { dress_code==WHITE; boys.size==5; } else if(days==MON && group==G3) { dress_code==WHITE; boys.size==3; } }

Thanks Sushrut.