Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help making random function faster

Status
Not open for further replies.

MaxGeek

Technical User
Jan 8, 2004
52
US
So I have a method that randomly places pieces on a board. The method uses the rejection method where it will create a random number to place the piece on the board, but if the place is already taken it will try again. The problem with this method is that if I have a lot of pieces to place it is very slow. (Like 40 pieces and 50 spaces)

How can I make this kind of method run faster no matter how many pieces or spaces there are?
 
Forget it, I think I've figured out a way to do it.
 
I would keep a list of all the available spaces - then use the random number to select one of the available numbers/spaces (ie like a subscript in an array) - keeping the random number below the number of actual spaces left - so you don't ever actually check if a space is taken, because you always generate a number for a space that exists.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top