Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Simplify _rand_exp #7

Open
jvoisin opened this issue Dec 16, 2019 · 2 comments
Open

Simplify _rand_exp #7

jvoisin opened this issue Dec 16, 2019 · 2 comments

Comments

@jvoisin
Copy link
Contributor

jvoisin commented Dec 16, 2019

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))?

@yevgenypats
Copy link

_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"

@jvoisin
Copy link
Contributor Author

jvoisin commented Dec 18, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants