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

Simplify _rand_exp #7

Open
Open

Description

@jvoisin

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

Activity

  1. yevgenypats commented on Dec 18, 2019

    @yevgenypats

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

  2. jvoisin commented on Dec 18, 2019

    @jvoisin
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions