Random numbers and functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deathwing
    New Member
    • Mar 2007
    • 32

    Random numbers and functions

    Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your comments and advice it's really helped me to learn. I have a new question that I an thinking about right now, I have an idea of how to proceed but wanted to pose this question to get some advice.

    Here is what I am trying to do and what I have figured out so far.
    • I then want the script to generate random numbers that are with the bounds of the "upper left and lower right" vectors/coordinates
    • So, basically none of the randomly generated decimal numbers can go out of the bounds of the "upper left and lower right" coordinates.
    • So far by playing around I have coded the following thanks to the knowledge gained from the help of others on this forum.


    Code:
    import random
    
    f = open(r "ni.txt", "w" )
    
    #defining num function
    
    def num(n):
          x,y = 150,200
          whil n < x and n < y:
                n += 1
                f.writelines(["%10.4f  %10.4f\n" % (random.random()*random.randrange(1,35),random.random()*random.randrange(1,35))])
    
    # calling function num
    num(random.randrange(1))
    
    f.close
    
    raw_input("\nPress ENTER to exit")
    
    #-----------------------------------------
    # output generated
    #-----------------------------------------
    
       10.8011     7.2231
        7.1643    27.9521
        9.6282     1.6043
        0.2221    33.7514
        8.5689    11.9781
       14.6153     5.3605
       21.9485     8.3382
        2.7590    12.3986
        0.5102     2.3836
        1.3386    19.3828
        0.9377     0.4229
        1.6024     1.5889
        8.4589     4.4812
       29.6486     3.9461
       12.8277     3.5010
       22.9659     4.1116
       17.1734    17.1360
        7.3082     1.6732
        0.8224     0.2964
        1.0680     5.9264
        8.6494     3.6651
        7.1057     5.5809
        2.4803    10.0084
        6.0981     7.9107
        2.0459     2.5169
        3.9406    13.4154
        0.2515     7.6117
        6.7652    28.2090
       11.9972     9.7133
       12.6741    10.6329
       28.5646     6.6041
        5.0474     3.1988
        1.8366    17.3606
    Now I know this is not the correct script but this is what I've got so far any advice/help would be greatly appreciated thanks everyone.

    Sincerely,
    Deathwing
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Deathwing
    Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your comments and advice it's really helped me to learn. I have a new question that I an thinking about right now, I have an idea of how to proceed but wanted to pose this question to get some advice.

    Here is what I am trying to do and what I have figured out so far.
    • I then want the script to generate random numbers that are with the bounds of the "upper left and lower right" vectors/coordinates
    • So, basically none of the randomly generated decimal numbers can go out of the bounds of the "upper left and lower right" coordinates.
    • So far by playing around I have coded the following thanks to the knowledge gained from the help of others on this forum.


    Code:
    import random
    
    f = open(r "ni.txt", "w" )
    
    #defining num function
    
    def num(n):
          x,y = 150,200
          whil n < x and n < y:
                n += 1
                f.writelines(["%10.4f  %10.4f\n" % (random.random()*random.randrange(1,35),random.random()*random.randrange(1,35))])
    
    # calling function num
    num(random.randrange(1))
    
    f.close
    
    raw_input("\nPress ENTER to exit")
    
    #-----------------------------------------
    # output generated
    #-----------------------------------------
    
       10.8011     7.2231
        7.1643    27.9521
        9.6282     1.6043
        0.2221    33.7514
        8.5689    11.9781
       14.6153     5.3605
       21.9485     8.3382
        2.7590    12.3986
        0.5102     2.3836
        1.3386    19.3828
        0.9377     0.4229
        1.6024     1.5889
        8.4589     4.4812
       29.6486     3.9461
       12.8277     3.5010
       22.9659     4.1116
       17.1734    17.1360
        7.3082     1.6732
        0.8224     0.2964
        1.0680     5.9264
        8.6494     3.6651
        7.1057     5.5809
        2.4803    10.0084
        6.0981     7.9107
        2.0459     2.5169
        3.9406    13.4154
        0.2515     7.6117
        6.7652    28.2090
       11.9972     9.7133
       12.6741    10.6329
       28.5646     6.6041
        5.0474     3.1988
        1.8366    17.3606
    Now I know this is not the correct script but this is what I've got so far any advice/help would be greatly appreciated thanks everyone.

    Sincerely,
    Deathwing
    Do not forget the '()' after 'f.close'! How about this:
    Code:
    import random
    
    x, y = 150, 200
    f = open('your_file', 'w')
    f.writelines(['%12.8f %14.8f\n' % (random.random()+random.randint(0,x-1), random.random()+random.randint(0, y-1)) for _ in range(35)])
    f.close()
    '''
    144.97567827    89.82142758
     54.80707963   105.65392938
    133.31185879   152.92742104
     64.71902430    58.47761159
    104.39356908   195.75766194
    ...........................................
    '''

    Comment

    • Deathwing
      New Member
      • Mar 2007
      • 32

      #3
      Originally posted by bvdet
      Do not forget the '()' after 'f.close'! How about this:
      Code:
      import random
      
      x, y = 150, 200
      f = open('your_file', 'w')
      f.writelines(['%12.8f %14.8f\n' % (random.random()+random.randint(0,x-1), random.random()+random.randint(0, y-1)) for _ in range(35)])
      f.close()
      '''
      144.97567827    89.82142758
       54.80707963   105.65392938
      133.31185879   152.92742104
       64.71902430    58.47761159
      104.39356908   195.75766194
      ...........................................
      '''
      hmmm not sure.. but I'll check it out thanks for the advice not 100% sure what the entire code means so I'll have to study it first hehehehehe... who knew programming would be such a learning curve hehehe

      Comment

      • Deathwing
        New Member
        • Mar 2007
        • 32

        #4
        Originally posted by bvdet
        Do not forget the '()' after 'f.close'! How about this:
        Code:
        import random
        
        x, y = 150, 200
        f = open('your_file', 'w')
        f.writelines(['%12.8f %14.8f\n' % (random.random()+random.randint(0,x-1), random.random()+random.randint(0, y-1)) for _ in range(35)])
        f.close()
        '''
        144.97567827    89.82142758
         54.80707963   105.65392938
        133.31185879   152.92742104
         64.71902430    58.47761159
        104.39356908   195.75766194
        ...........................................
        '''
        Hey dvbet thanks but I don't think that code is going to work. What I'm trying to do is designate an upper left coordinate and a lower right coordinate. Then within the bounds of those two coordinate I'm trying to generate a bunch of random numbers. So basically if you could picture a square grid in your mind , where you have an upperl left corner and a lower right corner.

        Then in your in generate a bunch of random points that eash have x and y coordinates within this gride. So far this is my logic, my "Upper left: x = 373362.31, y = 43695559.51 and my "lower right coordinates are: x = 622395.55, 4038425.26.

        This is why I was originally working on a function that has five variables I was thinking, x1, y1, x2, y2, and a num or n that would be used to produce all of the other random points.


        I'm still not sure how to get this but I'm still playing around with it

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by Deathwing
          Hey dvbet thanks but I don't think that code is going to work. What I'm trying to do is designate an upper left coordinate and a lower right coordinate. Then within the bounds of those two coordinate I'm trying to generate a bunch of random numbers. So basically if you could picture a square grid in your mind , where you have an upperl left corner and a lower right corner.

          Then in your in generate a bunch of random points that eash have x and y coordinates within this gride. So far this is my logic, my "Upper left: x = 373362.31, y = 43695559.51 and my "lower right coordinates are: x = 622395.55, 4038425.26.

          This is why I was originally working on a function that has five variables I was thinking, x1, y1, x2, y2, and a num or n that would be used to produce all of the other random points.


          I'm still not sure how to get this but I'm still playing around with it
          We can easily modify the code to work that way:
          Code:
          import random
          
          xUL = 373362.31
          yUL = 43695559.51
          xLR = 622395.55
          yLR = 4038425.26
          
          f = open(r'H:\TEMP\temsys\RandomNums3.txt', 'w')
          f.writelines(['%9.2f %14.2f\n' % (random.uniform(xUL,xLR), random.uniform(yLR, yUL)) for _ in range(35)])
          f.close()
          
          '''
          450742.25    15904885.61
          408010.58    17529876.24
          509410.49    31210428.63
          609751.61    28548191.36
          496748.10    13634931.19
          401620.04    18545059.27
          472709.78    19027787.41
          388559.39    24144909.11
          524567.31    18631123.05
          511758.70    39517663.45
          .....................................
          '''
          You can call me BV.

          Comment

          • Deathwing
            New Member
            • Mar 2007
            • 32

            #6
            Originally posted by bvdet
            We can easily modify the code to work that way:
            Code:
            import random
            
            xUL = 373362.31
            yUL = 43695559.51
            xLR = 622395.55
            yLR = 4038425.26
            
            f = open(r'H:\TEMP\temsys\RandomNums3.txt', 'w')
            f.writelines(['%9.2f %14.2f\n' % (random.uniform(xUL,xLR), random.uniform(yLR, yUL)) for _ in range(35)])
            f.close()
            
            '''
            450742.25    15904885.61
            408010.58    17529876.24
            509410.49    31210428.63
            609751.61    28548191.36
            496748.10    13634931.19
            401620.04    18545059.27
            472709.78    19027787.41
            388559.39    24144909.11
            524567.31    18631123.05
            511758.70    39517663.45
            .....................................
            '''
            You can call me BV.
            wow! thanks again BV, to be honest I don't completely understand all of that code so as usual I will have to study it but I'm sure I can figure out the madness behind your extremely logical and efficient code writing , hehehehe.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Deathwing
              wow! thanks again BV, to be honest I don't completely understand all of that code so as usual I will have to study it but I'm sure I can figure out the madness behind your extremely logical and efficient code writing , hehehehe.
              You are welcome. Thank YOU for sharing your problem with us and for the feedback.

              Comment

              • Deathwing
                New Member
                • Mar 2007
                • 32

                #8
                Originally posted by bvdet
                You are welcome. Thank YOU for sharing your problem with us and for the feedback.
                Hey okay... so I got a new question related to this one ? What if I want to allow the user to enter their own UL x and y and own LR x and y coordinates ? How would I go about doing that ? Here is what I have figured out so far.

                Code:
                import random
                
                print\
                       """
                                            #---------------#
                                            #    RPG 1.0    #
                                            #---------------#
                                            
                Welcome to Rndom_Point_Generator 1.0(RPG 1.0). RPG is very simple to use.
                RPG generates a series of random points and writes these points to a .xyz
                file.  In order to generate these points RPG asks the user to input two sets
                of variables, an upper_left and lower_right set of coordinates. It then
                proceeds to generate random points within the bounds of the upper_left and
                lower_right coordinate values.
                      """
                
                
                
                
                print "\nPlease enter the upper left coordinates\n"
                xUL = float(raw_input("enter your x value: "))
                yUL = float(raw_input("enter you y value: "))
                
                print "\nPlease enter the lower right coordinates\n"
                xLR = float(raw_input("enter your x value: "))
                yLR = float(raw_input("enter your y value: "))
                
                
                f = open(r'xyz.txt', 'w')
                
                
                f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform (yLR,yUL))for _ in range(35)])
                
                
                f.close()
                
                
                raw_input("\nPress Enter to exit.")
                I know it's not much different at all from what you've already explained BV but I am able to get the user to enter input I just can't get an output.

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by Deathwing
                  Hey okay... so I got a new question related to this one ? What if I want to allow the user to enter their own UL x and y and own LR x and y coordinates ? How would I go about doing that ? Here is what I have figured out so far.

                  Code:
                  import random
                  
                  print\
                         """
                                              #---------------#
                                              #    RPG 1.0    #
                                              #---------------#
                                              
                  Welcome to Rndom_Point_Generator 1.0(RPG 1.0). RPG is very simple to use.
                  RPG generates a series of random points and writes these points to a .xyz
                  file.  In order to generate these points RPG asks the user to input two sets
                  of variables, an upper_left and lower_right set of coordinates. It then
                  proceeds to generate random points within the bounds of the upper_left and
                  lower_right coordinate values.
                        """
                  
                  
                  
                  
                  print "\nPlease enter the upper left coordinates\n"
                  xUL = float(raw_input("enter your x value: "))
                  yUL = float(raw_input("enter you y value: "))
                  
                  print "\nPlease enter the lower right coordinates\n"
                  xLR = float(raw_input("enter your x value: "))
                  yLR = float(raw_input("enter your y value: "))
                  
                  
                  f = open(r'xyz.txt', 'w')
                  
                  
                  f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform (yLR,yUL))for _ in range(35)])
                  
                  
                  f.close()
                  
                  
                  raw_input("\nPress Enter to exit.")
                  I know it's not much different at all from what you've already explained BV but I am able to get the user to enter input I just can't get an output.
                  f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,XLR),rando m.uniform

                  SHOULD BE:

                  f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,xLR),rando m.uniform

                  Python is case sensitive.

                  Comment

                  • Deathwing
                    New Member
                    • Mar 2007
                    • 32

                    #10
                    Originally posted by bvdet
                    f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,XLR),rando m.uniform

                    SHOULD BE:

                    f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,xLR),rando m.uniform

                    Python is case sensitive.
                    sorry, that was a typo on my part. I now have the problem that the program seems to work but the ".txt" file that is created is blank empty, not filled in.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by bvdet
                      f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,XLR),rando m.uniform

                      SHOULD BE:

                      f.writelines(['9.2f%14.2f\n' % (random.uniform (xUL,xLR),rando m.uniform

                      Python is case sensitive.
                      I noticed another typo:

                      f.writelines(['%9.2f%14.2f\n' % (random.uniform (xUL,xLR),rando m.uniform

                      You had a missing '%'.

                      Comment

                      • Deathwing
                        New Member
                        • Mar 2007
                        • 32

                        #12
                        Originally posted by bvdet
                        I noticed another typo:

                        f.writelines(['%9.2f%14.2f\n' % (random.uniform (xUL,xLR),rando m.uniform

                        You had a missing '%'.

                        awk... stupid me! thanks BV I had no idea typo's could be so evil in python hehehehe. I'll be more careful next time. :)

                        Comment

                        • Deathwing
                          New Member
                          • Mar 2007
                          • 32

                          #13
                          Originally posted by Deathwing
                          awk... stupid me! thanks BV I had no idea typo's could be so evil in python hehehehe. I'll be more careful next time. :)
                          I've now been experimenting with the command line, how could I convert this so that it would work on the command line ? I looked at the sys module and getopt but don't quite understand them? Suggestions ?

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by Deathwing
                            I've now been experimenting with the command line, how could I convert this so that it would work on the command line ? I looked at the sys module and getopt but don't quite understand them? Suggestions ?
                            Do you mean that you would like to replace raw_input() with command-line arguments?

                            Code:
                            import sys
                            print sys.argv
                            will show all the command-line arguments pass when the script was invoked:

                            > pyhon theScript.py arg1 arg2 arg_n

                            Comment

                            • Deathwing
                              New Member
                              • Mar 2007
                              • 32

                              #15
                              Originally posted by bartonc
                              Do you mean that you would like to replace raw_input() with command-line arguments?

                              Code:
                              import sys
                              print sys.argv
                              will show all the command-line arguments pass when the script was invoked:

                              > pyhon theScript.py arg1 arg2 arg_n
                              Thanks bartonc ! that's a good suggestions I came up with this code that works what do you think ? Any other ways I could have acomplished it ?


                              Code:
                              import sys,random
                              
                              if len(sys.argv) !=6:
                                  sys.exit("You must enter at least 5 values")
                              
                              xUL = float(sys.argv[1])
                              yUL = float(sys.argv[2])
                              xLR = float(sys.argv[3])
                              yLR = float(sys.argv[4])
                              points = int(sys.argv[5])
                              
                              
                              
                              f = open(r'points.xyz', 'w')
                              
                              
                              f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
                              
                              
                              f.close()
                              
                              print '\nThanks for using RPG 1.0, your "points.xyz" file should now have been created.'
                              
                              raw_input("\nPress Enter to exit.")

                              Comment

                              Working...