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.
- 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
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.
- 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: - 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
-
...........................................
-
'''
Do not forget the '()' after 'f.close'! How about this: - 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
Do not forget the '()' after 'f.close'! How about this: - 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
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: - 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.
We can easily modify the code to work that way: - 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.
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.
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. -
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.
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. -
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),random.uniform
SHOULD BE:
f.writelines(['9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform
Python is case sensitive.
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.
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 '%'.
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. :)
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 ?
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? -
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
Do you mean that you would like to replace raw_input() with command-line arguments? -
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 ? - 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.")
-
-
-
-
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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..
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |