| Login | | Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name. | |
| Who's Online | There are currently, 52 guest(s) and 0 member(s) that are online.
You are Anonymous user. You can register for free by clicking here | |
 | |
|
Verification Guild: Forums |
|
| View previous topic :: View next topic |
| Author |
Message |
Shail Senior


Joined: Mar 18, 2005 Posts: 44
|
Posted: Mon Mar 05, 2012 10:54 am Post subject: select idx based on val via constraint from non-rand array |
|
|
Hi,
I have a dynamic array that holds index and value. I want to randomly pick index from list of indices that has a specific value. How can I do that in constraint ?
| Code: |
//procedurally
int pick_idx_array[] //array to keep index that are eligible to be picked
int pick_idx_index ; //index to the pick_idx_array whose corresponding value is the final choie
int pick_idx ; //final index picked
foreach (array[idx]) begin
if [array[idx] == val) pick_idx_array.push_back(idx);
end
pick_idx_index = $urandom_range(0,(pick_idx_array.size -1));
pick_idx = pick_idx_array[pick_idx_index];
| [/code] |
|
| Back to top |
|
 |
dave_59 Senior


Joined: Jun 22, 2004 Posts: 974 Location: Fremont, CA
|
Posted: Tue Mar 06, 2012 12:07 am Post subject: |
|
|
If the only random variable is the final index, you should use the inside operator: | Code: | function void pre_randomize();
pick_idx_array = array.find_index with ( item = val );
endfunction
constraint final_index { pick_idx inside {pick_idx_array};}
|
|
|
| Back to top |
|
 |
dave_59 Senior


Joined: Jun 22, 2004 Posts: 974 Location: Fremont, CA
|
Posted: Tue Mar 06, 2012 3:39 am Post subject: |
|
|
I just thought of a way to do this with everything random, without pre_randomize(), and no intermediate arrays.
| Code: | constraint sizes {array.size() inside {[1:20]};
pick_idx inside {[0:array.size()-1]};}
constraint indexes {foreach (array[i]) array[i] != val -> pick_idx != i;}
|
Dave Rich
Mentor Graphics |
|
| Back to top |
|
 |
Shail Senior


Joined: Mar 18, 2005 Posts: 44
|
Posted: Tue Mar 06, 2012 11:33 pm Post subject: |
|
|
Hi Dave,
Solution works great.. Thanks. !! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|