Randc type to packed and unpacked array

I have tried the following code

class random_test;
  randc bit [1:0][1:0] a;
  randc bit [1:0] b [1:0];
endclass

module test_top;
  random_test t = new();
  int i;
  initial begin
    repeat(20) begin
      void'(t.randomize());
      
      $display("No.%0d random value a = (%p, %p), b = %p", i, t.a[0], t.a[1], t.b);
      i++;
    end
  end
endmodule

and got the similar result from vcs, questa, xcelium

No.0 random value a = (1, 0), b = '{'h1, 'h1} 
No.1 random value a = (2, 0), b = '{'h0, 'h2} 
No.2 random value a = (0, 0), b = '{'h2, 'h3} 
No.3 random value a = (0, 1), b = '{'h3, 'h0} 
No.4 random value a = (3, 0), b = '{'h3, 'h1} 
No.5 random value a = (3, 3), b = '{'h0, 'h3} 
No.6 random value a = (1, 2), b = '{'h1, 'h2} 
No.7 random value a = (2, 3), b = '{'h2, 'h0} 
No.8 random value a = (2, 2), b = '{'h1, 'h0} 
No.9 random value a = (0, 2), b = '{'h3, 'h3} 
No.10 random value a = (0, 3), b = '{'h2, 'h1} 
No.11 random value a = (3, 2), b = '{'h0, 'h2} 
No.12 random value a = (1, 3), b = '{'h3, 'h3} 
No.13 random value a = (1, 1), b = '{'h0, 'h1} 
No.14 random value a = (2, 1), b = '{'h1, 'h2} 
No.15 random value a = (3, 1), b = '{'h2, 'h0} 
No.16 random value a = (2, 0), b = '{'h0, 'h3} 
No.17 random value a = (1, 0), b = '{'h3, 'h0} 
No.18 random value a = (0, 0), b = '{'h2, 'h2} 
No.19 random value a = (3, 0), b = '{'h1, 'h1} 

It seems that the element in a is repeated with the whole solution space, as the Dave stated in Randc in an variable within an array of objects - #3 by dave_59, which means the value of a could be the same until 16 randomize, while the value in b is repeated with each element itself, so it could be the same after 4 randomize.

I saw the description from LRM that

Arrays can be declared rand or randc, in which case all of their member elements are treated as
rand or randc.

and

An unpacked structure can be declared rand, in which case all of that structure’s random members
are solved concurrently using one of the rules listed in this subclause. Unpacked structures shall not
be declared randc.

From these description I would expect that unpacked array b got compile error and packed array a behaved like what array b did, yet they didn’t.

I would like to know where to find the definition of random behavior that packed and unpacked array declared as randc.

Thanks

Why do you think you should receive a compiler error?

I do think the LRM needs a clarification, which I just filed.

The LRM section 18.4 Random variables says

Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.

But it is not clear how randc applies to packed arrays. For packed structs, it says

A packed structure can be declared rand or randc, in which case that structure is treated as an integral type

I believe this applies to packed arrays as well, even when there are multiple dimensions of packed arrays and packed arrays of packed structures. Then entire packed component is treated as a singular integral value.

Hi Dave

Why do you think you should receive a compiler error?

Because I declared b as unpacked array, but LRM says

Unpacked structures shall not be declared randc.

It also used the word “shall not” to the dist as below, which leads to compile error. So I assume that if an unpacked array be declared as randc would leads to compile error too.

A dist operation shall not be applied to randc variables

Is my understanding incorrect?

===============

A packed structure can be declared rand or randc, in which case that structure is treated as an integral type

Does it mean that randc bit [1:0][1:0] a is interpreted as a randc variable, which has the possible value from 0~15? So that the value of (a[0], a[1]) will repeat only after 16 times randomize, the same as simulators do.

And how about unpacked type randc array b? It seems that it acts as {randc bit [1:0] b[1], randc bit [1:0] b[0]}, which means b[1] and b[0] has the possible value from 0~3 and will repeat only after 4 times randomize.

If “no compile error” to unpacked type randc array b is in expectation, where could I find the description for its random behavior?

Thanks

An unpacked structure (struct) is not an array.

Hi Dave,

So the word “unpacked structure” is refer to 7.2 Structures, rath than unpacked array, right?

And according to LRM

Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.

It already explained the random behavior of unpacked array, which cyclically random each element.

And for packed array,

A packed structure can be declared rand or randc, in which case that structure is treated as an integral type

If this concept of packed structure also applied to packed arrays, which interpreted it as an integer, then the random behavior would be cyclically random to this integer.

And one more question,

is the answer here correct?

randc int unsigned array1[6];

constraint c{
    foreach(array1[i])
      array1[i] < 7;
}

the array1 is an unpacked array, so its random behavior would be cyclically random to each element.

Therefore, we won’t got the following result

array1 = '{'h0, 'h0, 'h0, 'h0, 'h0, 'h0}
array1 = '{'h0, 'h0, 'h0, 'h0, 'h0, 'h1}
array1 = '{'h0, 'h0, 'h0, 'h0, 'h1, 'h0}
array1 = '{'h0, 'h0, 'h0, 'h0, 'h0, 'h2}

because array1[0] should be cyclic itself in each randomize, which means it couldn’t get 'h0 in 2nd randomize.

I tried the similar code

randc int unsigned f[2];

constraint c {
    foreach(f[i])
      f[i] inside {[0:3]};
}

and I get

No.0 random value f = (3, 3)
No.1 random value f = (2, 1) 
No.2 random value f = (1, 0)
No.3 random value f = (0, 2) 
No.4 random value f = (3, 0) 
No.5 random value f = (1, 2) 
No.6 random value f = (2, 1) 
No.7 random value f = (0, 3) 
No.8 random value f = (2, 1)
No.9 random value f = (3, 3) 
No.10 random value f = (0, 0)
No.11 random value f = (1, 2)

Thank you for clarification.