Randomising the chance that something will appear

BlitzMax Forums/BlitzMax Programming/Randomising the chance that something will appear

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Well as the thread title says, I need to randomise the chance that something will appear. I've done this before when I like say 2% of the time a bonus may appear, and that's fine. But now I need to say that something WILL appear but it will be one of 5 items. Each of those items has a predefined % chance that it will appear.

So for example:

A = 10%
B = 10%
C = 10%
D = 20%
E = 50%

Previously I've done things like populated and array with 1xA, 1xB, 1xC, 2xD, 5xE and then I've randomised a number from 1 to 10 and picked an array slot. That works fine, but if I want to use decimal places for my % chance then I'd have to make an array 1000+ or more slots long which is silly.

OK so I could do this:

1) Create a random number from 0 to 100.
2) Start a loop going and test if the random number is in the range of A, and if not check B and so on until E has been checked (well E does not need to be checked because if the random number is not in the range of D, we know it's in the range of E)

This seems a bit slow though, especially if I have LOTS of the items generating each logic loop.

Can anyone think of a quicker way? A Select Case would be pretty much the same as running a loop.

Thanks!