# Tackling a constraint in post\_randomize()

**URL:** <https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205>\
**Category:** SystemVerilog\
**Tags:** constraint-randomization, systemverilog-constraint\
**Created:** [May 2, 2024, 10:41pm UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205 "2024-05-02T22:41:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![VerifEngg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@VerifEngg](https://verificationacademy.com/forums/u/VerifEngg)\
**Post date:** [May 2, 2024, 10:41pm UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/1 "2024-05-02T22:41:00Z")

</div>

Hi All,  
I need to write a constraint for 32 bit address, to generate continuous 10 bits as 0 and remaining bits as 1.

I can do it two ways:

```auto
//No post_randomize use
class Example;
	rand bit[31:0] num;
	rand int start_bit;

	constraint c0{ start_bit inside {[0:22]};}
	constraint c2 {num[start_bit+:9] ==0;}
    constraint c3 {foreach(num[i]) {
		if (!(i inside {[start_bit:start_bit+9]})) 
			num[i] == 1;
	}};

endclass 

//Use of post_randomize()
class Example;
	rand bit[31:0] num;
	rand int start_bit;
    constraint c0{ start_bit inside {[0:22]};}
    constraint c1 {foreach(num[i]) {
			num[i] == 1;
	}};

	function void post_randomize();
		num[start_bit+:9] = 0;
	endfunction

endclass

```

Is one way better than the other or it does not matter? Thanks in advance for your input.

---

<div class="post-metadata">

**Author:** ![gsulliva](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/g/eb9ed0/32.png) [@gsulliva](https://verificationacademy.com/forums/u/gsulliva)\
**Post date:** [May 3, 2024, 6:00pm UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/2 "2024-05-03T18:00:16Z")

</div>

A simpler way to do the post\_randomize code is declare only one variable as “rand”

```auto
class Example;
    bit [31:0] num;
    rand int start_bit;
    constraint c0 {start_bit inside {[0:22]};}

    function void post_randomize;
        num = '1;
        num[start_bit +: 9] = 0;
    endfunction
endclass

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 4, 2024, 5:25am UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/3 "2024-05-04T05:25:54Z")

</div>

I suggest avoiding `pre/post_randomize()` if possible. It is easier to manage all constraint dependencies that way.

If you are going to iterate of all bits of `num` anyways, it would be simpler to write

```auto
constraint c0{ start_bit inside {[0:22]};}
constraint c3 {foreach(num[i]) 
		if (i inside {[start_bit:start_bit+9]})
			num[i] == 1;
        else 
			num[i] == 0;
	}

```

---

<div class="post-metadata">

**Author:** ![Lalita1701](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@Lalita1701](https://verificationacademy.com/forums/u/Lalita1701)\
**Post date:** [October 27, 2024, 2:59pm UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/4 "2024-10-27T14:59:45Z")

</div>

> [@VerifEngg](#):
>
> I need to write a constraint for 32 bit address, to generate continuous 10 bits as 0 and remaining bits as 1

I tried with a different approach:

```auto
class abc;
  
  rand bit[31:0] addr;
  rand bit[31:0] val;
  constraint addr_c{
    val inside {[1:21]};
   addr == 10'b1111_1111_11 << val;
  }

    function void post_randomize();
      addr = ~addr;
      $display("value of array = %b val =%0p",addr, val);
    endfunction   
endclass: abc

module tb;
   abc abc_inst;
  initial begin 
    abc_inst = new();
    repeat(10)
    abc_inst.randomize();

  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![nitin62goel](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nitin62goel](https://verificationacademy.com/forums/u/nitin62goel)\
**Post date:** [August 8, 2025, 5:22am UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/5 "2025-08-08T05:22:17Z")

</div>

you can use below constraints

```auto
rand bit [31:0] array;
bit [32:0] array_n;

function pre_randomize();
   array_n = array <<1;
endfunction

constraint cnt_c {
                               $countones(array) == 22;
                               $countones(array^array_n) == 2;
                              }

```

---

<div class="post-metadata">

**Author:** ![AGS91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/f475e1/32.png) [@AGS91](https://verificationacademy.com/forums/u/AGS91)\
**Post date:** [August 8, 2025, 5:52pm UTC](https://verificationacademy.com/forums/t/tackling-a-constraint-in-post-randomize/47205/6 "2025-08-08T17:52:09Z")

</div>

> [@nitin62goel](#):
>
> you can use below constraints
> 
> ```auto
> rand bit [31:0] array;
> bit [32:0] array_n;
> 
> function pre_randomize();
> array_n = array <<1;
> endfunction
> 
> constraint cnt_c {
> $countones(array) == 22;
> $countones(array^array_n) == 2;
> }
> 
> ```

@nitin62goel Have you tested this code ? I observe Constraint Solver issue (due to conflicting constraints) across all EDA tools
