Sometimes an example is just enough. Honestly, I do search and copy a lot. It is simply because I cannot remember everything. Sometimes I pay a lot of time to understand and get the code run perfectly and then completely forget about it after 1 year. This post is to store some of Systemverilog std::randomize examples that I created/collected, just for me to copy later. :D
Description
Code
Link
systemverilog std::randomize array with constrains on unique element, number of elements, array sum, constrains for each element value using foreach
intd_array[];// also works with queue d_array[$]std::randomize(d_array)with{unique{d_array};d_array.size()==10;d_array.sum==150;foreach(d_array[i]){d_array[i]<100;d_array[i]>1;d_array[i]!=i;}};
systemverilog std::randomize array using sum with
intd_array[];//also work with queue, try d_array[$]std::randomize(d_array)with{d_array.size()==5;d_array.sum()with(item>10?item:0)==100;foreach(d_array[i]){d_array[i]inside{[0:100]};}};
systemverilog std::randomize using implication constraints
systemverilog std::randomize with weight distribution
intd_array[];std::randomize(d_array)with{d_array.size()==1000;foreach(d_array[i]){d_array[i]dist{[0:50]:/80,[51:99]:/20};}};// 80% d_array element value will be in range [0:50]
systemverilog std::randomize with multi-dimensional array, constraint sum of all element, size for each dimension
intd_array[][];std::randomize(d_array)with{d_array.sum()with(item.sum()with(item))==1000;// total value of the arrayd_array.size()==10;foreach(d_array[i]){d_array[i].size()inside{[2:10]};}foreach(d_array[i,j]){d_array[i][j]inside{[0:100]};}};
systemverilog std::randomize with multi-dimensional array, 2nd example
systemverilog std::randomize with associative array, using enum as index
typedefenum{red=1,green,blue,pink,yellow}color_e;intd_array[color_e]='{red:10,green:20,blue:30,pink:40,yellow:50};//must construct the array element before randomizing.functionvoiddisplay();std::randomize(d_array)with{unique{d_array};foreach(d_array[i]){d_array[i]<50;d_array[i]>0;}};
systemverilog std::randomize with enum variable
typedefenum{RED=11,GREEN=22,BLUE=33,PINK=44,YELLOW=55}color_e;color_em_color;color_em_2nd_color;functionvoiddisplay();std::randomize(m_2nd_color,m_color)with{m_colorinside{RED,GREEN,YELLOW};// redundant code, no need to declare this below constraint// the constraint solver only selects the value inside the set of enum labelsm_2nd_colorinside{m_2nd_color};};
systemverilog std::randomize with 2 variable of 2 difference objects