# Why is the variable a not randomizing with given constraints?

**URL:** <https://verificationacademy.com/forums/t/why-is-the-variable-a-not-randomizing-with-given-constraints/40315>\
**Category:** SystemVerilog\
**Tags:** systemverilog-distribution-constraints, SystemVerilog, uvm-sv-constraints, logic\
**Created:** [April 26, 2022, 1:41pm UTC](https://verificationacademy.com/forums/t/why-is-the-variable-a-not-randomizing-with-given-constraints/40315 "2022-04-26T13:41:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [April 26, 2022, 1:52pm UTC](https://verificationacademy.com/forums/t/why-is-the-variable-a-not-randomizing-with-given-constraints/40315/2 "2022-04-26T13:52:24Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/why-is-the-variable-a-not-randomizing-with-given-constraints/40315):_

It’s working after adding the following:

```verilog
class packet;
 
	rand bit [6:0] num;
 
	randc bit [6:0] a;
 
	rand enum {EVEN,ODD} number;
 
	constraint c1 {a inside {35,36,40,45,[52:128]};}
 
	constraint c2 {num == func(a);}
 
	function bit [6:0] func(bit[6:0]a); //edited part
 
		if(a%2 == 0)
 
			begin
 
				number = EVEN;
 
				$display("\nThe number is %0s and %0d",number.name(),a);
 
			end
 
		else if(a%2 != 0)
 
			begin
 
				number = ODD;
 
				$display("\nThe number is %0s and %0d",number.name(),a);
 
			end		
 
		endfunction
 
endclass: packet
 
 
 
 
module even_odd_number_functional_constraint;
 
	initial 
 
		begin
 
			packet pkt = new;
 
			repeat (5)
 
				begin
 
					pkt.randomize();
 
				end
 
		end
 
endmodule: even_odd_number_functional_constraint

```

I want to know why there is a need to again declare the data type and the size of bits of ‘a’ inside function despite it being a global variable to the class packet.

---

_[View the full topic](https://verificationacademy.com/forums/t/why-is-the-variable-a-not-randomizing-with-given-constraints/40315)._
