Efficient random generator for very large range (in python)

前端 未结 3 457
你的背包
你的背包 2021-01-01 23:24

I am trying to create a generator that returns numbers in a given range that pass a particular test given by a function foo. However I would like the numbers to

3条回答
  •  滥情空心
    2021-01-01 23:58

    That may be a case where the best algorithm depends on the value of num, so why not using 2 selectable algorithms wrapped in one generator ?

    you could mix your shuffle and set solutions with a threshold on the value of num. That's basically assembling your 2 first solutions in one generator:

    from random import shuffle,randint
    
    def MyGenerator(foo, num):
        if num < 100000 # has to be adjusted by experiments
          order = list(range(num))
          shuffle(order)
          for i in order:
              if foo(i):
                  yield i
        else:   # big values, few collisions with random generator 
          tried = set()
          while len(tried) < num:
            i = randint(0, num-1)
            if i in tried:
               continue
            tried.add(i)
            if foo(i):
               yield i
    

    The randint solution (for big values of num) works well because there aren't so many repeats in the random generator.

提交回复
热议问题