Std::randomize question

I have a code where a variable is declared inside task. How do i constraint a below such that it doesnt have back 2 back value of 15? There are other tasks in the same class which will have their own local variable.



class test;
  
  task hello();
    int a;
    
    std::randomize(a) with {a inside {0,4'b1111};};
    $display ("a value is %d",a);                                  
    
  endtask
  
endclass

module tb;
  test t1;
  
  initial begin
    t1 = new();
    repeat (10) begin
    //if (!t1.randomize()) $display ("Error");
      t1.hello();  
  end
  end
  
endmodule


In reply to rag123:

One possible solution ::


class test;
  
  int prev_a ;
  
  task hello( inout int prev_a );
    int a;    // automatic variable , will  exist  per call .

   if( std::randomize(a) with {a inside {0,4'b1111}; a != prev_a ; } ) 
     begin
      prev_a = a ; 
      $display ("a value is %d",a);
    end  
  else
      $display (" Randomization Failed ");

  endtask
 
endclass
 
module tb;
  test t1;
  
  initial begin
   int previous_a ;
  
    t1 = new();
    repeat (10) begin
      
    //if (!t1.randomize()) $display ("Error");
      t1.hello( previous_a ); 
      
  end
  end
 
endmodule

In reply to ABD_91:

correction to the constraint:

if( std::randomize(a) with {a inside {0,4’b1111}; prev_a == 15 → a!= 15; } )

In reply to ABD_91:

another way through constraints:

class abc;
  rand int a;
  
  constraint c1 { 
    const'(a) == 15 -> a != const'(a);
    a inside {0,15};
  }
endclass

In reply to yourcheers:
but there is a compl err:
keyword const is not expected to be used in this context
my code is :
std::randomize(a) with {const’(a) == 7 → a == 0;};

Thanks ABD :) I slightly modified it. but mostly your code fits my need.



// Code your testbench here
// or browse Examples
class test;
 
 // int prev_a ;
 
  task hello(  );
    int a,prev_a;    // automatic variable , will  exist  per call .
 
    if( std::randomize(a) with {a inside {0,4'b1111}; if (a==4'b1111)a != prev_a ; } ) 
     begin
      prev_a = a ; 
      $display ("a value is %d",a);
    end  
  else
      $display (" Randomization Failed ");
 
  endtask
 
endclass
 
module tb;
  test t1;
 
  initial begin
  
 
    t1 = new();
    repeat (10) begin
 
    //if (!t1.randomize()) $display ("Error");
      t1.hello(  ); 
 
  end
  end
 
endmodule


In reply to liuxiaole:

this doesn’t work in scope randomization, its working only in class based rand variable constraints.