# \[help\] any good way to randomize compound object?

**URL:** <https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733>\
**Category:** OVM\
**Tags:** OVM\
**Created:** [April 1, 2010, 10:59am UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733 "2010-04-01T10:59:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchou](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/82dd89/32.png) [@yhchou](https://verificationacademy.com/forums/u/yhchou)\
**Post date:** [April 1, 2010, 10:59am UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/1 "2010-04-01T10:59:44Z")

</div>

Hi all,

is there any good way to randomize compound object? which means there are object(s) inside an object.

Experience shows sometimes the sub object also randomized when invoke c\_obj.randomize(), but sometimes nothing happens… is there any good way to do it? or is there any documentation talking about this, please give me some hints.

thanks a lot!

---

<div class="post-metadata">

**Author:** ![Vaibhav\_Tekale](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vaibhav_tekale/32/2015_2.png) [@Vaibhav\_Tekale](https://verificationacademy.com/forums/u/Vaibhav_Tekale)\
**Post date:** [April 1, 2010, 11:08am UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/2 "2010-04-01T11:08:13Z")

</div>

Hi,

You can use rand\_mode(0/1) to enable disable randomization of sub object.

Regards,

---

<div class="post-metadata">

**Author:** ![yhchou](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/82dd89/32.png) [@yhchou](https://verificationacademy.com/forums/u/yhchou)\
**Post date:** [April 1, 2010, 12:26pm UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/3 "2010-04-01T12:26:31Z")

</div>

As following example, is there any way that could generate the same kind of randomized compound object without inserting an customized “gen()” method? thanks!

you could ran this example by using  
irun -ovm -sv a.v

> `include "ovm.svh" class a extends ovm_object; rand int int_array[]; rand int int_array_size; constraint c_int_array_size { int_array_size < 40; int_array_size > 10; int_array.size() == int_array_size; } `ovm\_object\_utils\_begin(a)  
> `ovm_field_array_int (int_array, OVM_ALL_ON) `ovm\_object\_utils\_end  
> function new (string name = “a”);  
> super.new(name);  
> endfunction : new  
> endclass: a  
> class aa extends ovm\_object;  
> rand a a\_array;  
> rand int a\_array\_size;  
> constraint c\_a\_array\_size {  
> a\_array\_size \< 40; a\_array\_size \> 10;  
> a\_array.size() == a\_array\_size;  
> }  
> `ovm_object_utils_begin(aa) `ovm\_field\_int (a\_array\_size, OVM\_ALL\_ON)  
> `ovm\_object\_utils\_end  
> function new(string name = “aa”);  
> super.new(name);  
> endfunction : new  
> function gen();  
> foreach (a\_array[i]) a\_array[i] = a::type\_id::create(“a\_array”, null);  
> foreach (a\_array[i]) a\_array[i].randomize();  
> endfunction : gen  
> endclass  
> module tb;  
> aa aa\_obj;  
> a a\_array2;  
> initial begin  
> aa\_obj = aa::type\_id::create(“aa\_obj”, null);  
> aa\_obj.randomize();  
> $display(“size: %d”, aa\_obj.a\_array.size());  
> aa\_obj.gen();  
> a\_array2 = aa\_obj.a\_array;  
> foreach( a\_array2[i] )  
> $display(“- size: %d”, a\_array2[i].int\_array.size());  
> end  
> endmodule

---

<div class="post-metadata">

**Author:** ![Vaibhav\_Tekale](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vaibhav_tekale/32/2015_2.png) [@Vaibhav\_Tekale](https://verificationacademy.com/forums/u/Vaibhav_Tekale)\
**Post date:** [April 1, 2010, 12:58pm UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/4 "2010-04-01T12:58:43Z")

</div>

Hi ,

Instead of gen you can do this using post\_randomize function.

> function void post\_randomize();  
> foreach (a\_array[i]) a\_array[i] = a::type\_id::create(“a\_array”, null);  
> foreach (a\_array[i]) void’(a\_array[i].randomize());  
> endfunction : post\_randomize

Regards,

---

<div class="post-metadata">

**Author:** ![yhchou](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/82dd89/32.png) [@yhchou](https://verificationacademy.com/forums/u/yhchou)\
**Post date:** [April 1, 2010, 11:37pm UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/5 "2010-04-01T23:37:13Z")

</div>

Thanks for the help, using post\_randomize is bettern than the customized one.

however, is ther any reason why OVM could not also provide the field automation macro for array object to make sub object array randomize automatically?

---

<div class="post-metadata">

**Author:** ![Vaibhav\_Tekale](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vaibhav_tekale/32/2015_2.png) [@Vaibhav\_Tekale](https://verificationacademy.com/forums/u/Vaibhav_Tekale)\
**Post date:** [April 2, 2010, 4:53am UTC](https://verificationacademy.com/forums/t/help-any-good-way-to-randomize-compound-object/26733/6 "2010-04-02T04:53:30Z")

</div>

Hi,

This is not OVM limitation its SV language limitation.

The randomize function in SV doesn’t allocate the class object and its obvious it should not ALLOCATE class objects as your constructor arguments may differ for each class object.

Regards,
