This is for SystemVerilog. I know you can specify weights for values, or ranges of values, in the set of values that a random variable chooses from, but what if you wa开发者_如何学运维nt a nice Gaussian distribution? How do you write that kind of constraint?
When randomize is called, this class will generate values for variable "value" with a normal (Gaussian) distribution whose mean and standard deviation are 100 and 20, respectively. I haven't tested this much but it should work.
class C;
int seed = 1;
rand int mean;
rand int std_deviation;
rand int value;
function int gaussian_dist();
return $dist_normal( seed, mean, std_deviation );
endfunction
constraint c_parameters {
mean == 100;
std_deviation == 20;
}
constraint c_value { value == gaussian_dist(); }
endclass
As I'm unable to add a comment I've to write what looks like a new answer but probably isn't.
The code given by Steve K didn't work in VCS G-2012.09 (with service pack) due to the following issues:
mean
andstd_deviation
used ingaussian_dist()
should not berand
variables. I have just initialised them in the example below, but they can also be assigned inpre_randomize()
which is called before any randomisations.gaussian_dist()
is not allowed to modify variables other than those local to the function. The$dist_normal
call modifiesseed
so as a workaroundseed
can be made into an argument for the function.
Here is similar code with the issues resolved:
class C;
int seed = 1;
int mean = 100;
int std_deviation = 20;
rand int value;
function int gaussian_dist (int seed);
return $dist_normal (seed, mean, std_deviation);
endfunction
constraint c_value { value == gaussian_dist (seed); }
endclass
However the drawback of this code is that the new "seed" value given by $dist_normal
is thrown away, and for a subsequent randomisation the user has to set the seed
variable somehow (since with the same seed
value $dist_normal
would give the same output).
One option would be to use pre_randomize()
or post_randomize()
to randomise a Gaussian variable instead of putting it in constraint
blocks.
精彩评论