Verification Guild
A Community of Verification Professionals

 Create an AccountHome | Calendar | Downloads | FAQ | Links | Site Admin | Your Account  

Login
Nickname

Password

Security Code: Security Code
Type Security Code
BACKWARD

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.

Modules
· Home
· Downloads
· FAQ
· Feedback
· Recommend Us
· Web Links
· Your Account

Advertising

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

 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile  ProfileDigest    Log inLog in 

select idx based on val via constraint from non-rand array

 
Post new topic   Reply to topic    Verification Guild Forum Index -> Main
View previous topic :: View next topic  
Author Message
Shail
Senior
Senior


Joined: Mar 18, 2005
Posts: 44

PostPosted: Mon Mar 05, 2012 10:54 am    Post subject: select idx based on val via constraint from non-rand array Reply with quote

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
View user's profile
dave_59
Senior
Senior


Joined: Jun 22, 2004
Posts: 974
Location: Fremont, CA

PostPosted: Tue Mar 06, 2012 12:07 am    Post subject: Reply with quote

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
View user's profile Send e-mail Visit poster's website
dave_59
Senior
Senior


Joined: Jun 22, 2004
Posts: 974
Location: Fremont, CA

PostPosted: Tue Mar 06, 2012 3:39 am    Post subject: Reply with quote

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
View user's profile Send e-mail Visit poster's website
Shail
Senior
Senior


Joined: Mar 18, 2005
Posts: 44

PostPosted: Tue Mar 06, 2012 11:33 pm    Post subject: Reply with quote

Hi Dave,

Solution works great.. Thanks. !!
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    Verification Guild Forum Index -> Main All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
Verification Guild © 2006 Janick Bergeron
Web site engine's code is Copyright © 2003 by PHP-Nuke. All Rights Reserved. PHP-Nuke is Free Software released under the GNU/GPL license.
Page Generation: 0.172 Seconds