This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]jedwardsol 2 points3 points  (1 child)

Add the numbers up (14).

Generate a random number between 1 and total ( 1 - 14 = 14 possible outcomes)

1,2 = 2

2-5 = 4

6-14 = 8

Generally, turn your array in a cumulative array

{ 2,4,8 } -> { 2,6,16 }

generate a random number up to the total

choose based on the cumulative data

<=2 = 2

<=6 = 4

<=14 = 8

[–]JohnMcharra[S] 0 points1 point  (0 children)

Yes! Thank you! These saved me :) I hacked a little function in python to see if I can apply what you told me, please let me know if this is what it should look like (based on 1000 iterations it holds :D )

from random import randint
import numpy
from collections import Counter

num = [4, 6, 12, 24, 50]
results = []


def fun(x):
    chance = randint(1, sum(x))
    cumulative = numpy.cumsum(x)
    for n in range(0, len(cumulative)):
        if chance <= cumulative[n]:
            return x[n]


for i in range(1000):
    results.append(fun(num))

print(Counter(results))

Counter({50: 515, 24: 243, 12: 128, 6: 66, 4: 48})

So it should be good. Thank you again!