pythonfuzz icon indicating copy to clipboard operation
pythonfuzz copied to clipboard

Simplify _rand_exp

Open jvoisin opened this issue 6 years ago • 2 comments

The current _rand_exp is currently looking suboptimal:

    # Exp2 generates n with probability 1/2^(n+1).                               
    @staticmethod                                                                
    def _rand_exp():                                                                                                                                                                   
        rand_bin = bin(int(random.random() * 2**32-1))[2:]                       
        rand_bin = '0'*(32 - len(rand_bin)) + rand_bin                           
        count = 0                                                                
        for i in rand_bin:                                                       
            if i == '0':                                                         
                count +=1                                                        
            else:                                                                
                break                                                            
        return count

Am I misreading the code and the comment, or is it equivalent to int(random.expovariate(0.5))?

jvoisin avatar Dec 16 '19 22:12 jvoisin

_rand_exp generates a number from 0-32 I'm don't see how it's equivalent to int(random.expovariate(0.5)) but maybe I'm missing something. I also searched a better way to implement this when I ported this part from go-fuzz but didn't find anything "built-in"

yevgenypats avatar Dec 18 '19 18:12 yevgenypats

Then the comment above the function should be fixed/improved I think :)

jvoisin avatar Dec 18 '19 18:12 jvoisin