Quantcast
Channel: VBForums - Maths Forum
Viewing all articles
Browse latest Browse all 112

[RESOLVED] Round-about way to get even distribution using Random functions.

$
0
0
I have a list of entities that can be targeted. Lets the list has 10 entities and the entity doing the targeting selects one at random. Its a simple matter of rnd.Next(0,10) which gives each of the 10 entities a 1 in 10 chance of being targeted and random functions in VB produce a pretty evenly distributed set.

The problem here is that entities die all the time which means that they have to be removed from the list and when many entities are targeting many entities, that becomes a very expensive operation, so my solution is not to actually remove the entities when they die. I gave them a simply property which determines if they are still alive. This means that I have to change the targeting logic. I cannot simply use rnd.Next(0,10) because it may select a dead entity and it would require me to select again and this can really become a problem if there are a large number of dead entities in the list. This can cause an indeterministic performance hit due to all the reselects.

My idea is to enumerate the list whenever a random target is requested and generate a random number on each iteration and if the number chosen is equal to a certain number and the entity selected is not dead then return it as a target. Now I was thinking of two ways. Both ways involve testing the entity's property to see if its still alive first and then generating the random number to determine if it should be chosen. My 1st idea is to use rnd.Next(0,1) and choose it if the number chosen is 1 which gives each each living entity a 50% chance of being chosen. Problem is that I suspect that entities higher on the list would get chosen more times than entities lower in the list hence not an even distribution. My 2nd idea is to use rnd.next(0,n) where n is the number of entities in the list, or I may use the number of living entities. This returns the original odds of each entity being chosen in the case of a list of 10 entities to 1 in 10. However, I suspect that a good number of times, no entity will be chosen in which case I will simply return the last one in the list which makes me suspect that the last entity will be chosen most of the time, hence not an even distribution.

I just want an idea of how I can use this method of iteration to get an even enough distribution.

Viewing all articles
Browse latest Browse all 112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>