Advanced random number generator?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shing
    New Member
    • Mar 2007
    • 58

    Advanced random number generator?

    I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.

    For example:

    I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8 ,9.

    How must I go about this?
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by shing
    I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.

    For example:

    I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8 ,9.

    How must I go about this?
    you can use the random module. Look here and here

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by shing
      I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.

      For example:

      I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8 ,9.

      How must I go about this?
      >>> import random as rand
      >>> help(rand)
      Help on module random:

      NAME
      random - Random variable generators.

      FILE
      d:\python24\lib \random.py
      >>> rand.sample([1,2,3,4,5,6,7,8 ,9], 5)
      [8, 4, 5, 7, 6]
      >>>

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by shing
        I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.

        For example:

        I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8 ,9.

        How must I go about this?
        Code:
        >>> import random
        >>> seq = [1,2,3,4,5,6,7,8,9]
        >>> n = 7
        >>> int(''.join([str(random.choice(seq)) for i in range(n)]))
        3683319
        >>>

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bvdet
          Code:
          >>> import random
          >>> seq = [1,2,3,4,5,6,7,8,9]
          >>> n = 7
          >>> int(''.join([str(random.choice(seq)) for i in range(n)]))
          3683319
          >>>
          That's very nice. How 'bout
          Code:
          >>> int("".join(str(i) for i in rand.sample([1,2,3,4,5,6,7,8,9], 5)))

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by bartonc
            That's very nice. How 'bout
            Code:
            >>> int("".join(str(i) for i in rand.sample([1,2,3,4,5,6,7,8,9], 5)))
            I am stuck in 2.3, so no generator expressions - only list comprehensions. random.sample() works great except it will not reuse the numbers and you cannot ask for more numbers than are in the sample list.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by bvdet
              I am stuck in 2.3, so no generator expressions - only list comprehensions. random.sample() works great except it will not reuse the numbers and you cannot ask for more numbers than are in the sample list.
              Here's what I had in mind:
              Code:
              >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
              >>> i
              71725441469245314962196254882137L
              >>> int_format(str(i))
              '71,725,441,469,245,314,962,196,254,882,137'
              >>>

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by bvdet
                Here's what I had in mind:
                Code:
                >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
                >>> i
                71725441469245314962196254882137L
                >>> int_format(str(i))
                '71,725,441,469,245,314,962,196,254,882,137'
                >>>
                So, what library (your own?) are those functions from?

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by bartonc
                  So, what library (your own?) are those functions from?
                  It's somethig I wrote:
                  Code:
                  import random
                  
                  def int_format(s):
                      if len(s)<=3:
                          return s
                      else:
                          outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
                          outList.reverse()
                          return ','.join([x for x in outList if len(x) > 0])
                  
                  def varRandom(dList, n):
                      num = ''.join([str(random.choice(dList)) for i in range(n)])
                      while num.startswith('0'):
                          num = num[1:]
                          num += str(random.choice(dList))
                      return int(num)
                  
                  i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
                  print i
                  print int_format(str(i))
                  
                  print int_format('12345678923')
                  print int_format('1234567892')
                  print int_format('123456789')
                  print int_format('12345678')
                  
                  '''
                  >>> 46010601035631449157303537548986
                  46,010,601,035,631,449,157,303,537,548,986
                  12,345,678,923
                  1,234,567,892
                  123,456,789
                  12,345,678
                  >>>
                  '''
                  This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit:
                  Code:
                  def int_format(s):
                      if len(s) <= 3:
                          return s
                      return int_format(s[:-3]) + "," + s[-3:]
                  I wanted to write my own but could not match the elegance.

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by bvdet
                    It's somethig I wrote:
                    Code:
                    import random
                    
                    def int_format(s):
                        if len(s)<=3:
                            return s
                        else:
                            outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
                            outList.reverse()
                            return ','.join([x for x in outList if len(x) > 0])
                    
                    def varRandom(dList, n):
                        num = ''.join([str(random.choice(dList)) for i in range(n)])
                        while num.startswith('0'):
                            num = num[1:]
                            num += str(random.choice(dList))
                        return int(num)
                    
                    i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
                    print i
                    print int_format(str(i))
                    
                    print int_format('12345678923')
                    print int_format('1234567892')
                    print int_format('123456789')
                    print int_format('12345678')
                    
                    '''
                    >>> 46010601035631449157303537548986
                    46,010,601,035,631,449,157,303,537,548,986
                    12,345,678,923
                    1,234,567,892
                    123,456,789
                    12,345,678
                    >>>
                    '''
                    This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit:
                    Code:
                    def int_format(s):
                        if len(s) <= 3:
                            return s
                        return int_format(s[:-3]) + "," + s[-3:]
                    I wanted to write my own but could not match the elegance.
                    A slight modification might be an improvement:
                    Code:
                    def int_format(s, sep=","):
                        if len(s) <= 3:
                            return s
                        return int_format(s[:-3]) + sep + s[-3:]
                    for use in Europe where the comma is not so common.

                    BTW, how do you like your new status?

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by bartonc
                      A slight modification might be an improvement:
                      Code:
                      def int_format(s, sep=","):
                          if len(s) <= 3:
                              return s
                          return int_format(s[:-3]) + sep + s[-3:]
                      for use in Europe where the comma is not so common.

                      BTW, how do you like your new status?
                      No pressure, huh? I noticed it today and assumed there was a problem with the website. I was thinking it should be TBE (training to be an expert).

                      Comment

                      Working...