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

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.

In reply to bonny1989:


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…};


//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}

In reply to ssureshg_:

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

In reply to bonny1989:

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


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];
};

In reply to ssureshg_:

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.

In reply to bonny1989:
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.

In reply to dave_59:
Oh Okay.
Just tried, didn’t work with VCS but works fine Questa.
Thanks Dave!!

In reply to bonny1989:

In reply to dave_59:
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…

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…

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