Constraint to repeat a number at every third time

In reply to syed taahir ahmed:

Hi ,

These 2 Solutions work



// Sample 1

class A ;

rand bit [3:0] varr ; // Unconstrained by Default

endclass


module Main ;


A a1 ;



initial begin


a1 = new();

for ( int i = 1 ; i < 10 ; i++ ) // Loop 
begin

if ( i % 3 != 0 )
begin

if ( a1.randomize() with { a1.varr != 5 ; } ) // In-Line Constraint
  $display("Success with a1 == %p",a1); 

end
else if ( i % 3 == 0 )
begin

if ( a1.randomize() with { a1.varr == 5 ; } ) // In-Line Constraint 
  $display("Factor of 3 with a1 == %p",a1); 


end

end

end

endmodule

// Sample 2 

class A ;

rand bit [3:0] varr ;

bit [3:0] dummy_varr ; // To be Used as a Constraint Guard 

constraint C {  
                if ( ( dummy_varr % 3 ) == 0 ) // Constraint Guard
                {
		    varr == 5 ;
		}
                
                else 
                {
		   varr != 5 ; 
		}

            }		
endclass


module Main ;


A a1 ;



initial begin


a1 = new();

for ( int i = 1 ; i < 10 ; i++ )
begin

a1.dummy_varr = i ;

if ( a1.randomize() )
  $display("Success with a1 == %p",a1); 


end

end

endmodule



Note the above 2 Codes Make sure that for other times ( Not divisible by 3 ) we Never get 5

Regards ,

AGIS