Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 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
Contributor

@yevgenypats yevgenypats commented Dec 18, 2019

_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 jvoisin commented Dec 18, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.