# How to randomize a int variable with 2 mandatory values?

**URL:** <https://verificationacademy.com/forums/t/how-to-randomize-a-int-variable-with-2-mandatory-values/33771>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [December 2, 2017, 11:37am UTC](https://verificationacademy.com/forums/t/how-to-randomize-a-int-variable-with-2-mandatory-values/33771 "2017-12-02T11:37:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [December 2, 2017, 8:11pm UTC](https://verificationacademy.com/forums/t/how-to-randomize-a-int-variable-with-2-mandatory-values/33771/2 "2017-12-02T20:11:18Z")

</div>

_In reply to [Prat\_0693](https://verificationacademy.com/forums/t/how-to-randomize-a-int-variable-with-2-mandatory-values/33771):_  
The issue I see is that there is a sequence flow in your requirements.  
Below is a solution based on my understanding of your requirements. Perhaps this can guide you into the approach you need.

```verilog

import uvm_pkg::*; `include "uvm_macros.svh" 
class C;
	/* I want to randomize a variable "a" such that even if I randomize this 2 times or more, 
	 * I should get atleast 1 "maxval" and 1 "minval".
     For ex. `define aint 4 `define 10 10
     I want to randomize variable a only on the basis of `aint (i.e only 4 times) 
     and I am required that I should get atleast one "0" and one "10" 
     (depending on the value of `10) and the rest two values of a can be between 0 and 10.
     [Ben] Not clear requirements. Let me assume th following requirements: 
     1) within aint (e.g., 4) randomize calls, and within those 4 times, 
     2) there should be at least one 0 and one 10 */
     // `define aint 4
    // `define 10 10
	int count; 
	bit found_min; 
	bit found_max; 
   rand int unsigned a;	
   function void do_randomize(); 
   	case (count)
   		0, 1 : begin :a01
   			  count += 1'b1;
   			  if (!randomize(a) with {a >=0; a<11;}) `uvm_error("MYERR", "This is a randomize error")
   			  if(a==0) found_min=1'b1; 
   			  if(a==10) found_max=1'b1;
   		    end :a01 	
   		2,3 : begin :a23
   			      if(!found_min && !found_max) begin 
   		    	    if (!randomize(a) with 
   		    			{a dist {0:=1, 10:=1};
   		    			}) `uvm_error("MYERR", "This is a randomize error")
   		    	    if(a==0) found_min=1'b1; 
   		    	    if(a==10) found_max=1'b1;
   		    	  end
   		    	  else if(found_min && !found_max) begin 
   		    	  	if (!randomize(a) with 
   		    	  			{a dist {0:=0, 10:=1};
   		    	  			}) `uvm_error("MYERR", "This is a randomize error")
   		    	    //if(a==0) found_min=1'b1; 
   		    	    if(a==10) found_max=1'b1;
   		    	  end
   		    	  else if(found_min && !found_max) begin 
   		    		if (!randomize(a) with 
   		    				{a dist {0:=1, 10:=0};
   		    				}) `uvm_error("MYERR", "This is a randomize error")
   		    		if(a==0) found_min=1'b1; 
   		    		//if(a==10) found_max=1'b1;
   		    	   end   	
   		    	  else begin :a23e
   		    	  	if (!randomize(a) with 
   		    			{ a dist {1'b1:=1, 1'b0:=3};
   		    			}) `uvm_error("MYERR", "This is a randomize error")
   		          
   		            if(count !=3) begin 
   		              if(a==0) found_min=1'b1; 
   		               if(a==10) found_max=1'b1;     		        
   		               count += 1'b1;
   		             end
   		            else begin 
   		              count=0;   		        
   		    	      found_min=1'b0; 
   		    	      found_max=1'b0;   		    	
   		    	    end 
   		           end :a23e
   		        end :a23
   		    default : $display("error in case"); 
   	endcase
   		    
     endfunction :do_randomize 
endclass

module top; 
	timeunit 1ns; timeprecision 100ps;  
	bit clk;
	int a; 
	C c; 
	initial forever #10 clk=!clk;  
 
	initial begin 
	c=new(); 
		repeat(200) begin 
			@(posedge clk);   
			c.do_randomize(); 
			a=c.a; 
			$display("a %d, count %d, found_min %b, found_max %b", c.a, c.count, c.found_min, c.found_max);
			
		end 
		$stop; 
	end 
endmodule  

# a 3, count 1, found_min 0, found_max 0
# a 6, count 2, found_min 0, found_max 0
# a 10, count 2, found_min 0, found_max 1
# a 0, count 3, found_min 1, found_max 1
# a 0, count 0, found_min 0, found_max 0
# a 6, count 1, found_min 0, found_max 0
# a 3, count 2, found_min 0, found_max 0
# a 0, count 2, found_min 1, found_max 0
# a 10, count 2, found_min 1, found_max 1
# a 1, count 3, found_min 1, found_max 1
# a 0, count 0, found_min 0, found_max 0
# a 6, count 1, found_min 0, found_max 0
# a 2, count 2, found_min 0, found_max 0
# a 0, count 2, found_min 1, found_max 0
# a 10, count 2, found_min 1, found_max 1
# a 0, count 3, found_min 1, found_max 1
# a 1, count 0, found_min 0, found_max 0
# a 8, count 1, found_min 0, found_max 0
# a 10, count 2, found_min 0, found_max 1
# a 1, count 3, found_min 0, found_max 1
# a 0, count 0, found_min 0, found_max 0
# a 6, count 1, found_min 0, found_max 0
# a 4, count 2, found_min 0, found_max 0
# a 0, count 2, found_min 1, found_max 0
# a 10, count 2, found_min 1, found_max 1
 

```

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)  
For training, consulting, services: contact [Home - My cvcblr](http://cvcblr.com/home)

- [SVA Handbook 4th Edition, 2016 ISBN 978-1518681448](http://goo.gl/JOfuEB)
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- [Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 978-1539769712](https://goo.gl/d10QHh)
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115

* * *

---

_[View the full topic](https://verificationacademy.com/forums/t/how-to-randomize-a-int-variable-with-2-mandatory-values/33771)._
