Generate a number which has sequence 11101

i would like to generate random no’s which has this sequence of bits (11101) to be present

In reply to abhiverif:

I am going to have to assume you mean you would like to generate random values which has this pattern of bits (11101) to be present within a larger value.
If so, then I suggest:

module top;
class A;
   rand bit [31:0] value;
   rand int unsigned position;
   bit [4:0] pattern = 5'b11101;
   parameter mask = {$bits(pattern){1'b1}};
   constraint c { (value & (mask << position)) == (pattern << position);
      position <= $bits(value)-$bits(pattern);}
endclass
   A a = new;
   initial repeat (30) begin
      assert(a.randomize());
      $display("%2d %b",a.position, a.value);
      end
endmodule

If not, then please give an example of results you would like to see.

In reply to dave_59:

thanks dave

In reply to abhiverif:
This worked for me:

constraint c { value == (pattern << position);
                position inside {[1:32]};};

Dave,
Can you please help me understand this:
parameter mask = {$bits(pattern){1’b1}}; //This means you are making the mask: 5’b11111 right … I am not understanding your constraint, can you please explain with a small example on what it is trying to do.

   constraint c { (value & (mask << position)) == (pattern << position);
     position <= $bits(value)-$bits(pattern);}

//when you says $bits(value) ,does it refer to the value of the 32 bit variable “value”.

In reply to sriram.seshagiri:

Your constraint has 2 problems.

The first is
position
must include
position==0
. It turns out the constraint on
position
is unnecessary because the other constraint on value cannot be satisfied with
position
outside the range 0-27 (Both yours and my examples).

The other is the bits outside of the pattern range need to have random values. Using your constraint, those bits will always be 0. My constraint masks out those bits so the comparison always matches 0 for bits outside the pattern.

In reply to dave_59:

Thanks Dave :) this is making more sense now.

In reply to dave_59:

Just curious. How to use constraints to generate a number([31:0]) which has random number of the pattern 5’b11101?

Thanks