'lo all! Got a couple specific scenarios I need to plan for. I have already built fully working code in both of these cases, but fully expect I did NOT go about it the right way! Here are the scenarios:
1. I have a series of items/objects and I need to select *some* of them at random. So a simple, real world example would be I have the numbers 1 to 100, and I need to randomly select 13 of those numbers. Right now the way I'm doing this is by choosing a normal random number between 1 and 100 and then seeing if it's a random number that has already been picked - if so, I re-randomly select a number and start the check again, and so on. The problem is this could theoretically cause an infinite loop, and I further suspect it's bad programming etiquette to build it this way.
2. This is a somewhat similar scenario, just with screen coordinates this time. I need to generate N number of random boxes on the screen (random sizes, colors, locations, etc.), but need to be sure none of them overlap. Once again, the way I'm doing this now is to create one box and then test for collision against every other box already built - if overlap then re-randomly generate the box. Again, this could theoretically cause an infinite loop and I'm sure it's a bad approach to solving this problem.
Thusly, I open the table to suggestions! Thanks all, Jason
1. I have a series of items/objects and I need to select *some* of them at random. So a simple, real world example would be I have the numbers 1 to 100, and I need to randomly select 13 of those numbers. Right now the way I'm doing this is by choosing a normal random number between 1 and 100 and then seeing if it's a random number that has already been picked - if so, I re-randomly select a number and start the check again, and so on. The problem is this could theoretically cause an infinite loop, and I further suspect it's bad programming etiquette to build it this way.
2. This is a somewhat similar scenario, just with screen coordinates this time. I need to generate N number of random boxes on the screen (random sizes, colors, locations, etc.), but need to be sure none of them overlap. Once again, the way I'm doing this now is to create one box and then test for collision against every other box already built - if overlap then re-randomly generate the box. Again, this could theoretically cause an infinite loop and I'm sure it's a bad approach to solving this problem.
Thusly, I open the table to suggestions! Thanks all, Jason