Randomization of multiple of N

I am using a variable, logic [7:0] in a class and I will do randomize().

I want a variable to be multiple of N. I should write constraints.

For example, I want a variable that is 2, 4, 6, 8, 10 … or 256 in a case. And I want a variable that is 4, 8, 12, 16 … or 256 in a case.

Writing constraints is hard for me. Help…

In reply to jh_veryveri:

(In my honest opinion and without wanting to sound rude) : It’s a good idea to show some code which demonstrates that you have tried solving this. If you are running into issues, you can then request help. Asking for complete solutions is not going to help you.

One of the possible solutions to your question is the below code. You can modify it to suit your use case.


class A;
  int n;
  
  rand logic [7:0] my_var;
  constraint my_var_c {my_var % n == 0;};
  
  function disp();
    $display("my_var is %0d", my_var);
  endfunction
endclass


module top;
  A a1 = new();
  
  initial begin
    for(int i = 1; i < 5; i++) begin
      a1.n = i;
      $display("Multiplier is %0d", i);
      repeat(10) begin
        a1.randomize();
        a1.disp();
      end
      
    end
    
    
  end
endmodule

In reply to KillSteal:

Thanks for your comment and code. And sorry.

I’m going to describe the details.

I have a derived class and a variable(logic [7:0]) is declared in the base class.
I want to describe a constraint for the variable using `uvm_do_with.


`uvm_do_with ( obj, {
    if ( some_case ) {
      // multiple of 2 constraint of the variable
    } else if ( other_case0 ) {
      // multiple of 4 constraint of the variable
    } else if ( other_case1 ) }
      // multiple of 8 constraint of the variable
    }
   ...
  }
)

I’m going to try to use your code.
Please consider what I describe.

Thanks you.