473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Random numbers and functions

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.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. f = open(r "ni.txt", "w" )
  4.  
  5. #defining num function
  6.  
  7. def num(n):
  8.       x,y = 150,200
  9.       whil n < x and n < y:
  10.             n += 1
  11.             f.writelines(["%10.4f  %10.4f\n" % (random.random()*random.randrange(1,35),random.random()*random.randrange(1,35))])
  12.  
  13. # calling function num
  14. num(random.randrange(1))
  15.  
  16. f.close
  17.  
  18. raw_input("\nPress ENTER to exit")
  19.  
  20. #-----------------------------------------
  21. # output generated
  22. #-----------------------------------------
  23.  
  24.    10.8011     7.2231
  25.     7.1643    27.9521
  26.     9.6282     1.6043
  27.     0.2221    33.7514
  28.     8.5689    11.9781
  29.    14.6153     5.3605
  30.    21.9485     8.3382
  31.     2.7590    12.3986
  32.     0.5102     2.3836
  33.     1.3386    19.3828
  34.     0.9377     0.4229
  35.     1.6024     1.5889
  36.     8.4589     4.4812
  37.    29.6486     3.9461
  38.    12.8277     3.5010
  39.    22.9659     4.1116
  40.    17.1734    17.1360
  41.     7.3082     1.6732
  42.     0.8224     0.2964
  43.     1.0680     5.9264
  44.     8.6494     3.6651
  45.     7.1057     5.5809
  46.     2.4803    10.0084
  47.     6.0981     7.9107
  48.     2.0459     2.5169
  49.     3.9406    13.4154
  50.     0.2515     7.6117
  51.     6.7652    28.2090
  52.    11.9972     9.7133
  53.    12.6741    10.6329
  54.    28.5646     6.6041
  55.     5.0474     3.1988
  56.     1.8366    17.3606
  57.  
  58.  
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
Mar 30 '07 #1
14 2618
bvdet
2,851 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. f = open(r "ni.txt", "w" )
  4.  
  5. #defining num function
  6.  
  7. def num(n):
  8.       x,y = 150,200
  9.       whil n < x and n < y:
  10.             n += 1
  11.             f.writelines(["%10.4f  %10.4f\n" % (random.random()*random.randrange(1,35),random.random()*random.randrange(1,35))])
  12.  
  13. # calling function num
  14. num(random.randrange(1))
  15.  
  16. f.close
  17.  
  18. raw_input("\nPress ENTER to exit")
  19.  
  20. #-----------------------------------------
  21. # output generated
  22. #-----------------------------------------
  23.  
  24.    10.8011     7.2231
  25.     7.1643    27.9521
  26.     9.6282     1.6043
  27.     0.2221    33.7514
  28.     8.5689    11.9781
  29.    14.6153     5.3605
  30.    21.9485     8.3382
  31.     2.7590    12.3986
  32.     0.5102     2.3836
  33.     1.3386    19.3828
  34.     0.9377     0.4229
  35.     1.6024     1.5889
  36.     8.4589     4.4812
  37.    29.6486     3.9461
  38.    12.8277     3.5010
  39.    22.9659     4.1116
  40.    17.1734    17.1360
  41.     7.3082     1.6732
  42.     0.8224     0.2964
  43.     1.0680     5.9264
  44.     8.6494     3.6651
  45.     7.1057     5.5809
  46.     2.4803    10.0084
  47.     6.0981     7.9107
  48.     2.0459     2.5169
  49.     3.9406    13.4154
  50.     0.2515     7.6117
  51.     6.7652    28.2090
  52.    11.9972     9.7133
  53.    12.6741    10.6329
  54.    28.5646     6.6041
  55.     5.0474     3.1988
  56.     1.8366    17.3606
  57.  
  58.  
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:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. x, y = 150, 200
  4. f = open('your_file', 'w')
  5. 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)])
  6. f.close()
  7. '''
  8. 144.97567827    89.82142758
  9.  54.80707963   105.65392938
  10. 133.31185879   152.92742104
  11.  64.71902430    58.47761159
  12. 104.39356908   195.75766194
  13. ...........................................
  14. '''
Mar 30 '07 #2
Do not forget the '()' after 'f.close'! How about this:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. x, y = 150, 200
  4. f = open('your_file', 'w')
  5. 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)])
  6. f.close()
  7. '''
  8. 144.97567827    89.82142758
  9.  54.80707963   105.65392938
  10. 133.31185879   152.92742104
  11.  64.71902430    58.47761159
  12. 104.39356908   195.75766194
  13. ...........................................
  14. '''
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
Mar 31 '07 #3
Do not forget the '()' after 'f.close'! How about this:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. x, y = 150, 200
  4. f = open('your_file', 'w')
  5. 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)])
  6. f.close()
  7. '''
  8. 144.97567827    89.82142758
  9.  54.80707963   105.65392938
  10. 133.31185879   152.92742104
  11.  64.71902430    58.47761159
  12. 104.39356908   195.75766194
  13. ...........................................
  14. '''
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
Apr 2 '07 #4
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. xUL = 373362.31
  4. yUL = 43695559.51
  5. xLR = 622395.55
  6. yLR = 4038425.26
  7.  
  8. f = open(r'H:\TEMP\temsys\RandomNums3.txt', 'w')
  9. f.writelines(['%9.2f %14.2f\n' % (random.uniform(xUL,xLR), random.uniform(yLR, yUL)) for _ in range(35)])
  10. f.close()
  11.  
  12. '''
  13. 450742.25    15904885.61
  14. 408010.58    17529876.24
  15. 509410.49    31210428.63
  16. 609751.61    28548191.36
  17. 496748.10    13634931.19
  18. 401620.04    18545059.27
  19. 472709.78    19027787.41
  20. 388559.39    24144909.11
  21. 524567.31    18631123.05
  22. 511758.70    39517663.45
  23. .....................................
  24. '''
You can call me BV.
Apr 2 '07 #5
We can easily modify the code to work that way:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. xUL = 373362.31
  4. yUL = 43695559.51
  5. xLR = 622395.55
  6. yLR = 4038425.26
  7.  
  8. f = open(r'H:\TEMP\temsys\RandomNums3.txt', 'w')
  9. f.writelines(['%9.2f %14.2f\n' % (random.uniform(xUL,xLR), random.uniform(yLR, yUL)) for _ in range(35)])
  10. f.close()
  11.  
  12. '''
  13. 450742.25    15904885.61
  14. 408010.58    17529876.24
  15. 509410.49    31210428.63
  16. 609751.61    28548191.36
  17. 496748.10    13634931.19
  18. 401620.04    18545059.27
  19. 472709.78    19027787.41
  20. 388559.39    24144909.11
  21. 524567.31    18631123.05
  22. 511758.70    39517663.45
  23. .....................................
  24. '''
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.
Apr 2 '07 #6
bvdet
2,851 Expert Mod 2GB
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.
Apr 2 '07 #7
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.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print\
  4.        """
  5.                             #---------------#
  6.                             #    RPG 1.0    #
  7.                             #---------------#
  8.  
  9. Welcome to Rndom_Point_Generator 1.0(RPG 1.0). RPG is very simple to use.
  10. RPG generates a series of random points and writes these points to a .xyz
  11. file.  In order to generate these points RPG asks the user to input two sets
  12. of variables, an upper_left and lower_right set of coordinates. It then
  13. proceeds to generate random points within the bounds of the upper_left and
  14. lower_right coordinate values.
  15.       """
  16.  
  17.  
  18.  
  19.  
  20. print "\nPlease enter the upper left coordinates\n"
  21. xUL = float(raw_input("enter your x value: "))
  22. yUL = float(raw_input("enter you y value: "))
  23.  
  24. print "\nPlease enter the lower right coordinates\n"
  25. xLR = float(raw_input("enter your x value: "))
  26. yLR = float(raw_input("enter your y value: "))
  27.  
  28.  
  29. f = open(r'xyz.txt', 'w')
  30.  
  31.  
  32. f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform (yLR,yUL))for _ in range(35)])
  33.  
  34.  
  35. f.close()
  36.  
  37.  
  38. raw_input("\nPress Enter to exit.")
  39.  
  40.  
  41.  
  42.  
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.
Apr 4 '07 #8
bvdet
2,851 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print\
  4.        """
  5.                             #---------------#
  6.                             #    RPG 1.0    #
  7.                             #---------------#
  8.  
  9. Welcome to Rndom_Point_Generator 1.0(RPG 1.0). RPG is very simple to use.
  10. RPG generates a series of random points and writes these points to a .xyz
  11. file.  In order to generate these points RPG asks the user to input two sets
  12. of variables, an upper_left and lower_right set of coordinates. It then
  13. proceeds to generate random points within the bounds of the upper_left and
  14. lower_right coordinate values.
  15.       """
  16.  
  17.  
  18.  
  19.  
  20. print "\nPlease enter the upper left coordinates\n"
  21. xUL = float(raw_input("enter your x value: "))
  22. yUL = float(raw_input("enter you y value: "))
  23.  
  24. print "\nPlease enter the lower right coordinates\n"
  25. xLR = float(raw_input("enter your x value: "))
  26. yLR = float(raw_input("enter your y value: "))
  27.  
  28.  
  29. f = open(r'xyz.txt', 'w')
  30.  
  31.  
  32. f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform (yLR,yUL))for _ in range(35)])
  33.  
  34.  
  35. f.close()
  36.  
  37.  
  38. raw_input("\nPress Enter to exit.")
  39.  
  40.  
  41.  
  42.  
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),random.uniform

SHOULD BE:

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

Python is case sensitive.
Apr 4 '07 #9
f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform

SHOULD BE:

f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.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.
Apr 4 '07 #10
bvdet
2,851 Expert Mod 2GB
f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,XLR),random.uniform

SHOULD BE:

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

Python is case sensitive.
I noticed another typo:

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

You had a missing '%'.
Apr 4 '07 #11
I noticed another typo:

f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.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. :)
Apr 4 '07 #12
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 ?
Apr 18 '07 #13
bartonc
6,596 Expert 4TB
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?

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.argv
will show all the command-line arguments pass when the script was invoked:

> pyhon theScript.py arg1 arg2 arg_n
Apr 19 '07 #14
Do you mean that you would like to replace raw_input() with command-line arguments?

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. 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 ?


Expand|Select|Wrap|Line Numbers
  1. import sys,random
  2.  
  3. if len(sys.argv) !=6:
  4.     sys.exit("You must enter at least 5 values")
  5.  
  6. xUL = float(sys.argv[1])
  7. yUL = float(sys.argv[2])
  8. xLR = float(sys.argv[3])
  9. yLR = float(sys.argv[4])
  10. points = int(sys.argv[5])
  11.  
  12.  
  13.  
  14. f = open(r'points.xyz', 'w')
  15.  
  16.  
  17. f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
  18.  
  19.  
  20. f.close()
  21.  
  22. print '\nThanks for using RPG 1.0, your "points.xyz" file should now have been created.'
  23.  
  24. raw_input("\nPress Enter to exit.")
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
Apr 19 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
5
by: drs | last post by:
Is there any way to generate random numbers based on arbitrary real valued functions? I am looking for something like random.gauss() but with natural log and exponential functions. thanks, -d
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
7
by: moni | last post by:
Hi, Can anyone tell me , how I would generate random numbers, for an interval say from 15 to 450 in C. Plz lemme know. thanx..
2
by: Steven D'Aprano | last post by:
I'm working on some functions that, essentially, return randomly generated strings. Here's a basic example: def rstr(): """Return a random string based on a pseudo normally-distributed random...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.