How to get a random number with 'weight'?

BlitzMax Forums/BlitzMax Programming/How to get a random number with 'weight'?

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 there are many ways to do this.

1) Use a wieghted SQL query (that don't help here)
2) Fill an array with the possibilities and choose at random from the array, for example.

Lets assume:

Red ball = 1 (weight 15)
Black ball = 2 (weight 3)
Blue Ball = 3 (Wieght 2)

So our array of numbers is :
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,3,3]

Choose a random position from the array and select the correct ball :)

This method is used widely.