mardi 24 décembre 2019

More efficient and general way of getting a random integer from multiple lists of different lengths

What I want is to be able to have mutable lists of mutable length and draw a random number from these lists. The trick is that these lists are different sizes so if a list is smaller than the others a number from it should not come up as often as from the other bigger lists.

Here's what I have now:

 import random

 a = 1
 b = 11
 c = 111
 d = 1111
 e = 11111
 f = 111111
 g = f - e
 q = (f - e) + (d - c) + (b - a)
 r = ((f - e) / q ) * g
 s = g - ((b - a) / q ) * g

 low = 1
 mid = 1
 high = 1

 i = 1
 while i < 666666:
     x = random.randint(a, b)
     y = random.randint(c, d)
     z = random.randint(e, f)
     v = random.randint(1, g)
     if v < r:
         print(z)
         high += 1
     else:
         if v > s:
             print(x)
             low += 1
         else:
             print(y)
             mid += 1
     i += 1

 print(low, mid, high)



  ...
  72027
  81188
  57 6579 660032

It works, but it's very crude and requires me to know which list is the longest, etc. Thanks.




Aucun commentaire:

Enregistrer un commentaire