# Can we randomize 2-D array using std::randomize()?

**URL:** <https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, assertstdrandomizevariable, array-randomization\
**Created:** [August 5, 2017, 11:58am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384 "2017-08-05T11:58:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonny1989](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bonny1989](https://verificationacademy.com/forums/u/bonny1989)\
**Post date:** [August 5, 2017, 11:58am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/1 "2017-08-05T11:58:24Z")

</div>

Hello,

I want to randomize 2-D array,  
let us say,  
rand data [3][3]

such that entire row and column won’t have any repeated value. (Just like Sudoku)  
In our case,  
1 3 2  
2 1 3  
3 2 1

But it should be with std::randomize() or any other run time randomization method but not object randomization.

---

<div class="post-metadata">

**Author:** ![ssureshgverifier](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@ssureshgverifier](https://verificationacademy.com/forums/u/ssureshgverifier)\
**Post date:** [August 6, 2017, 2:18am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/2 "2017-08-06T02:18:49Z")

</div>

_In reply to [bonny1989](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384):_

```verilog

class sudo;

rand bit[1:0] a[4][4];
rand bit[1:0] b[4][4];

constraint u_a0 {unique {a[0]}; } //unable to use foreach on this for some reason
constraint u_a1 {unique {a[1]}; }
constraint u_a2 {unique {a[2]}; }
constraint u_a3 {unique {a[3]}; }

constraint u_b0 {unique {b[0]}; }
constraint u_b1 {unique {b[1]}; }
constraint u_b2 {unique {b[2]}; }
constraint u_b3 {unique {b[3]}; }

constraint u_ab0 {foreach(a[i,j]) a[i][j] == b[j][i]; }

endclass:sudo

module sudoku();

sudo i_sudo = new();

initial begin
 repeat(3) begin
   i_sudo.randomize(); 
   foreach (i_sudo.a[i]) $display("a is %p", i_sudo.a[i]);
 end
end

endmodule:sudoku

```

> > but not object randomization ?? oops I did not see this…  
> > anyway copy-paste the constraints under std::randomize(a,b) with {…constraints here…};

---

<div class="post-metadata">

**Author:** ![ssureshgverifier](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@ssureshgverifier](https://verificationacademy.com/forums/u/ssureshgverifier)\
**Post date:** [August 6, 2017, 2:40am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/3 "2017-08-06T02:40:32Z")

</div>

```verilog

//constraint u_a0 {foreach (a[i]) unique {a[i]}; } //unable to use foreach on this for some reason

```

I would prefer something like above, instead of unrolling - your simulator might support…or am I missing something ?

output for 3 loops above

a is '{'h3, 'h1, 'h0, 'h2}  
a is '{'h2, 'h0, 'h3, 'h1}  
a is '{'h0, 'h2, 'h1, 'h3}  
a is '{'h1, 'h3, 'h2, 'h0}

a is '{'h1, 'h0, 'h3, 'h2}  
a is '{'h2, 'h3, 'h1, 'h0}  
a is '{'h3, 'h2, 'h0, 'h1}  
a is '{'h0, 'h1, 'h2, 'h3}

a is '{'h3, 'h2, 'h0, 'h1}  
a is '{'h2, 'h3, 'h1, 'h0}  
a is '{'h0, 'h1, 'h2, 'h3}  
a is '{'h1, 'h0, 'h3, 'h2}

---

<div class="post-metadata">

**Author:** ![bonny1989](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bonny1989](https://verificationacademy.com/forums/u/bonny1989)\
**Post date:** [August 8, 2017, 10:08am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/4 "2017-08-08T10:08:10Z")

</div>

_In reply to [ssureshg\_](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/3):_

Thanks a lot for response.  
But that is what I want without randomizing object.  
Please suggest if you have any other solution. :)

---

<div class="post-metadata">

**Author:** ![ssureshgverifier](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@ssureshgverifier](https://verificationacademy.com/forums/u/ssureshgverifier)\
**Post date:** [August 8, 2017, 4:19pm UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/5 "2017-08-08T16:19:52Z")

</div>

_In reply to [bonny1989](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/4):_

same thing will work with std::randomize, as you asked for

```verilog

std::randomize(a,b) with {
unique {a[0]}; //you can use foreach here - I have problem with simulator
unique {a[1]};
unique {a[2]};
unique {a[3]};
unique {b[0]};
unique {b[1]};
unique {b[2]};
unique {b[3]};
foreach(a[i,j]) a[i][j] == b[j][i];
};

```

---

<div class="post-metadata">

**Author:** ![bonny1989](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bonny1989](https://verificationacademy.com/forums/u/bonny1989)\
**Post date:** [September 4, 2017, 7:24am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/6 "2017-09-04T07:24:14Z")

</div>

_In reply to [ssureshg\_](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/5):_

Hello,

Thanks for your help. It worked with fixed size array but I feel it is not working wit dynamic 2-D array.

## Here is the below code I tried:

module top();  
int d;

initial begin  
d = new[4];

// Creating individual array index  
foreach(d[i])  
begin  
d[i]=new[4];  
end

```
// Randomizing array 
std::randomize(d);
...

```

## endmodule: top

## And it gave me below error:

## Error-[UARC] Unsupported argument to randomize call testbench.sv, 15 “d” Arg #1 of std::randomize “d” is unsupported mixed array

So, is it like that randomize doesn’t work with 2-D dynamic array?

Thanks.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 4, 2017, 6:06pm UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/7 "2017-09-04T18:06:21Z")

</div>

_In reply to [bonny1989](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/6):_  
An “unsupported” error message usually means what you are doing is correct, but the tool in use has not implemented it. Please contact your tool vendor.

---

<div class="post-metadata">

**Author:** ![bonny1989](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bonny1989](https://verificationacademy.com/forums/u/bonny1989)\
**Post date:** [September 5, 2017, 5:30am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/8 "2017-09-05T05:30:26Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/7):_  
Oh Okay.  
Just tried, didn’t work with VCS but works fine Questa.  
Thanks Dave!!

---

<div class="post-metadata">

**Author:** ![VC\_45](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@VC\_45](https://verificationacademy.com/forums/u/VC_45)\
**Post date:** [September 11, 2017, 5:30am UTC](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/9 "2017-09-11T05:30:34Z")

</div>

_In reply to [bonny1989](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/8):_

> _In reply to [dave\_59](https://verificationacademy.com/forums/t/can-we-randomize-2-d-array-using-std-randomize/33384/8):_  
> Oh Okay.  
> Just tried, didn’t work with VCS but works fine Questa.  
> Thanks Dave!!

Having the 2D array with **fixed size dimension type** seems to be working with std::randomize() with VCS…

```verilog
module top();
    int d[4][4];

    initial begin
        std::randomize(d);
    end
endmodule: top

```

And the 2D array with mixed dimension types is working from class based randomization…

```verilog
module top();
    class cls;
        rand int d[][];
        constraint cons_define_sizes {
            d.size == 4;
            foreach (d[i])
                d[i].size == 4;
        }

        constraint cons_rndomize_arr_mem {
            //...
        }
    endclass

    initial begin
        cls obj = new;
        obj.randomize;
    end
endmodule: top

```
