I’m trying to generate 2d randomized value within random-cyclic.
I came across 1d randomize reference. it’s used a “randc”.
So I made 2d with randc. but randc does not work.
class Packet;
randc bit [2:0] data;
endclass
module tb;
initial begin
Packet pkt = new ();
for (int i = 0 ; i < 10; i++) begin
pkt.randomize ();
$display ("itr=%0d data=0x%0h", i, pkt.data);
end
end
endmodule
This is 2d with randc. I though that randc can be used in 2d array but it doesn’t.
how can I make 2d array with randc only?
class test3_c;
rand logic [7:0] width;
rand logic [7:0] height;
randc logic [3:0] data[][];
constraint size_con {
width inside {[0:255]};
height inside {[0:255]};
data.size == width;
foreach(data[i])
data[i].size == width;
}
endclass
module test3;
initial begin
test3_c test;
test = new();
for(int i=0; i<3; i++)
for(int j=0; j<3; j++) begin
void'(test.randomize());
$display("data[%0d][%0d]=%0d", i,j,test.data[i][j]);
end
end
endmodule
In reply to UVM_LOVE:
There are several problems with your code.
You call randomize() inside the for-loop displaying the array. That means randomize() gets called many times before some elements are displayed. Call randomize() once before trying to display.
For displaying, the size of the array dimension could be less than three elements. Use a foreach-loop instead.
And resizing a dynamic array as part of randomizing destroys the previous array and reconstructs a new one. That reconstruction also destroys the random cycle states.
You probably need to come up with a practical use case for want you are trying to code.
In reply to dave_59:
Thank you Dave,
“You call randomize() inside the for-loop displaying the array. That means randomize() gets called many times before some elements are displayed. Call randomize() once before trying to display.”
class test3_c;
rand logic [7:0] width;
rand logic [7:0] height;
randc logic [2:0] arr_2d[][];
randc logic [2:0] queue_1d[$];
randc logic [2:0] arr_1d[];
randc logic [2:0] arr;
constraint size_con {
width inside {[0:255]};
height inside {[0:255]};
arr inside {[2:7]};
arr_1d.size == 3;
foreach(arr_1d[i]) {
arr_1d[i] > 1;
arr_1d[i] < 6;
}
}
//constraint uni {
// unique {arr_1d};
//}
endclass
module test3;
initial begin
test3_c test;
test = new();
//for arr;
void'(test.randomize());
for(int i=0; i<3; i++) begin
$display("data[%0d]=%0d", i,test.arr);
end
end
But if I do this, then I get the same values
data[0]=6
data[1]=6
data[2]=6
In reply to UVM_LOVE:
test.arr is not an array.
In reply to dave_59:
Thanks Dave But do I need to do something more about constraint to make array in ranc()?
I think arr_1d is array. so I declared it with “randc” and generate random numbers by foreach(). But same result I get.
arr_1d[0]=6
arr_1d[1]=5
arr_1d[2]=5
arr_1d[3]=1
I didn’t get it. why does this “randc” break?
class arr_test_c;
randc logic [2:0] arr; //This is not the array.
randc logic [2:0] arr_1d[];
constraint size_con {
arr inside {[2:4]};
arr_1d.size() inside {[3:4]};
}
endclass
module test;
initial begin
arr_test_c arr_test;
arr_test = new();
void'(arr_test.randomize());
foreach(arr_test.arr_1d[i]) begin
$display("arr_1d[%0d]=%0d", i, arr_test.arr_1d[i]);
end
end
endmodule
In reply to UVM_LOVE:
I think you are confusing uniqueness with cyclic randomization behavior.
Uniqueness says certain random variables must have different values in a single call to randomize(). The unique constraint is used for that.
Cyclic randomization means a random variable, must cycle through all possible values in each call to randomize() before repeating the same value. randc is used when declaring a cyclic random variable. You need more than one call to randomize() to see the effect of a cyclic, random variable