random.sample with long int items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jordi

    #1

    random.sample with long int items

    I need the random.sample functionality where the population grows up to
    long int items. Do you know how could I get this same functionality in
    another way? thanks in advance.
    Jordi

  • Paul Rubin

    #2
    Re: random.sample with long int items

    "jordi" <jpahullo@gmail .com> writes:[color=blue]
    > I need the random.sample functionality where the population grows up to
    > long int items. Do you know how could I get this same functionality in
    > another way? thanks in advance.[/color]

    Nothing stops you:
    [color=blue][color=green][color=darkred]
    >>> from random import sample
    >>> a = [n**25 for n in range(6)]
    >>> a[/color][/color][/color]
    [0, 1, 33554432, 847288609443L, 112589990684262 4L, 298023223876953 125L][color=blue][color=green][color=darkred]
    >>> sample(a,2)[/color][/color][/color]
    [112589990684262 4L, 298023223876953 125L][color=blue][color=green][color=darkred]
    >>> sample(a,2)[/color][/color][/color]
    [298023223876953 125L, 847288609443L][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Is this what you were asking, or did you mean something different?

    Comment

    • Steven D'Aprano

      #3
      Re: random.sample with long int items

      On Wed, 12 Apr 2006 06:29:01 -0700, jordi wrote:
      [color=blue]
      > I need the random.sample functionality where the population grows up to
      > long int items. Do you know how could I get this same functionality in
      > another way? thanks in advance.[/color]

      I'm thinking you might need to find another way to do whatever it is you
      are trying to do.

      If you can't, you could do something like this:

      - you want to randomly choose a small number of items at random from a
      population of size N, where N is very large.

      e.g. you would do this: random.sample(x range(10**10), 60)
      except it raises an exception.

      - divide your population of N items in B bins of size M, where both B and
      M are in the range of small integers. Ideally, all your bins will be equal
      in size.

      e.g.
      bins = [xrange(start*10 **5, (start+1)*10**5 ) \
      for start in xrange(10**5)]


      - then, to take a sample of n items, do something like this:

      # bins is the list of B bins;
      # each bin has M items, and B*M = N the total population.
      result = []
      while len(result) < sample_size:
      # choose a random bin
      bin = random.choice(b ins)
      # choose a random element of that bin
      selection = random.choice(b in)
      if selecting_with_ replacement:
      result.append(s election)
      else:
      # each choice must be unique
      if not selection in result:
      result.append(s election)


      Hope that helps.


      --
      Steven.

      Comment

      • Steven D'Aprano

        #4
        Re: random.sample with long int items

        On Wed, 12 Apr 2006 06:44:29 -0700, Paul Rubin wrote:
        [color=blue]
        > "jordi" <jpahullo@gmail .com> writes:[color=green]
        >> I need the random.sample functionality where the population grows up to
        >> long int items. Do you know how could I get this same functionality in
        >> another way? thanks in advance.[/color]
        >
        > Nothing stops you:
        >[color=green][color=darkred]
        > >>> from random import sample
        > >>> a = [n**25 for n in range(6)]
        > >>> a[/color][/color]
        > [0, 1, 33554432, 847288609443L, 112589990684262 4L, 298023223876953 125L][color=green][color=darkred]
        > >>> sample(a,2)[/color][/color]
        > [112589990684262 4L, 298023223876953 125L][/color]

        No, I think he means the size of the list is big enough to need a long
        int. Something like xrange(10**10) or even bigger.
        [color=blue][color=green][color=darkred]
        >>> random.sample(x range(10*10), 10)[/color][/color][/color]
        [96, 45, 90, 52, 57, 72, 94, 73, 79, 97][color=blue][color=green][color=darkred]
        >>> random.sample(x range(10**10), 10)[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        OverflowError: long int too large to convert to int


        --
        Steven.

        Comment

        • Paul Rubin

          #5
          Re: random.sample with long int items

          Steven D'Aprano <steve@REMOVETH IScyber.com.au> writes:[color=blue]
          > e.g. you would do this: random.sample(x range(10**10), 60)
          > except it raises an exception.[/color]

          For a population that large and a sample that small (less than
          sqrt(population size), the chance of collision is fairly small, so you
          can just discard duplicates.

          This relies on Python 2.4's randrange function to generate arbitrarily
          large ranges, which in turn relies on having getrandbits (new 2.4
          feature, thanks Ray) available:

          samp = Set()
          while len(samp) < 60:
          samp.add(random .randrange(10** 10))

          Comment

          • jordi

            #6
            Re: random.sample with long int items

            That is just what I need. I did't mind on 'divide and conquer' :(

            Thanks a lot!

            --
            Jordi

            Comment

            Working...