我试图随机化一个动态数组,使所有元素的和等于一个数字。 我写了一个代码如下:
class sim_cycles;
rand int unsigned sim_cyc[];
int unsigned total_sim_cycles = 1000;
rand int unsigned total_stripes;
constraint c_sim_cyc {
sim_cyc.size() == total_stripes;
//sim_cyc.sum() with (int'(item)) == total_sim_cycles; // This didnt make any difference
sim_cyc.sum() == total_sim_cycles;
foreach(sim_cyc[i]) {
sim_cyc[i] >= 50;
//sim_cyc[i] < total_sim_cycles;
}
}
endclass
vcs仿真输出为:
sim_cyc[0]: 2385876847
sim_cyc[1]: 217190463
sim_cyc[2]: 949546727
sim_cyc[3]: 1880026222
sim_cyc[4]: 3157295333
结果很奇怪对不对?
如果我添加约束sim_cyc [i] <total_sim_cycles,我会得到一个随机问题。
因为sim_cyc [0]碰巧等于1000,这与sim_cyc [i] <1000冲突。
其实,在上面的约束中,少了一项。作为最大值,每个sim_cyc元素不能大于total_sim_cycles。
需要将total_stripes限制为最少一个元素到一个合理的最大值。 你可能想要
constraint c_sim_cyc {
sim_cyc.size() == total_stripes;
total_stripes inside {1:10000};
sim_cyc.sum() with (longint'(item)) == total_sim_cycles;
foreach(sim_cyc[i]) {
sim_cyc[i] >= 50;
sim_cyc[i] <= total_sim_cycles;
}
}