61
Programming / Re: RNG theory question
« on: May 07, 2010, 01:08:27 PM »
amjh is right.
If you want to be 100% sure to never pick the same location twice you could try an algorithm like this:
I use a similar algorithm when I want to pick a random choice from a set of possibilities.
The only drawback is having a quite large list for large locations.
But as with most algorithms, you trade memory vs performance.
...But sometimes I just use the brute-force loop "roll rng until it gives a good answer"
If you want to be 100% sure to never pick the same location twice you could try an algorithm like this:
- 1. Generate a list of all locations. Order doesn't matter.
- 2. When you want one:
- 2.1 Pick one at random from the list (pick an index in the list with your usual rng).
- 2.2 Remove it from the list.
- 3. When you free a location, put it back in the list.
I use a similar algorithm when I want to pick a random choice from a set of possibilities.
The only drawback is having a quite large list for large locations.
But as with most algorithms, you trade memory vs performance.
...But sometimes I just use the brute-force loop "roll rng until it gives a good answer"
