Hello,
I'm fairly new to python and have only been playing around with it for one week actually. It's my first attempt at programming and I'm trying to generate some random floats and then have the results of this random generation be filled into a .txt file. So far I have only been able to generate random intergers and create a blank .txt. Can anyone help ? How do I get my ints to turn to floats I have tried using the "float" datatype infront of my numbers but that does not work. Also, how after having generated the random numbers do I get them to populate the .txt file that is created.
Thanks so very much
Here is my code thus far:
# Random number generator
# This script generates random numbers
# The script then writes the result to a text file
text_file = open("points.txt", "w")
#text_file.write(i)
import random
for numbers in range( 1, 201 ):
print "%5d" % ( random.randrange( 1, 201 ) ),
if numbers % 6 == 0:
print
#text_file.writelines(numbers)
text_file.close()
raw_input("\n\nPress the enter key to exit.")
13 2201 bvdet 2,851
Expert Mod 2GB
Hello,
I'm fairly new to python and have only been playing around with it for one week actually. It's my first attempt at programming and I'm trying to generate some random floats and then have the results of this random generation be filled into a .txt file. So far I have only been able to generate random intergers and create a blank .txt. Can anyone help ? How do I get my ints to turn to floats I have tried using the "float" datatype infront of my numbers but that does not work. Also, how after having generated the random numbers do I get them to populate the .txt file that is created.
Thanks so very much
Here is my code thus far:
# Random number generator
# This script generates random numbers
# The script then writes the result to a text file
text_file = open("points.txt", "w")
#text_file.write(i)
import random
for numbers in range( 1, 201 ):
print "%5d" % ( random.randrange( 1, 201 ) ),
if numbers % 6 == 0:
print
#text_file.writelines(numbers)
text_file.close()
raw_input("\n\nPress the enter key to exit.")
Maybe you can modify this to meet your requirements: - import random
-
-
# generate 20 random numbers and write to a text file
-
-
f = open(r'path\dir\file_name', 'w')
-
for i in range(20):
-
f.write('%.8f\n' % (random.random()))
-
-
f.close()
-
-
'''
-
0.49790300
-
0.22560614
-
0.94740230
-
0.76629025
-
0.54590250
-
0.01583565
-
0.87463924
-
0.39809160
-
0.06935606
-
0.69546613
-
0.79916955
-
0.53687077
-
0.56587108
-
0.78859956
-
0.91774516
-
0.63878437
-
0.38448525
-
0.60584063
-
0.30951708
-
0.56703802
-
'''
Please use code tags in your posts. See reply guidelines. HTH :)
bvdet 2,851
Expert Mod 2GB
Here is another way: - import random
-
-
f = open(r'path\dir\file_name', 'w')
-
f.writelines(['%.8f\n' % (random.random()) for _ in range(20)])
-
f.close()
Thanks bvdet, I'll take a look at it I hope I can modify it, it looks very interesting you have taught me something new todya great! :"D.
Sincerely,
Deathiwng
eg -
python -c "import random; print '\n'.join([str(random.randint(1,201)) for i in range(20)])" > file
-
eg -
python -c "import random; print '\n'.join([str(random.randint(1,201)) for i in range(20)])" > file
-
could you explain? I'm sorry I don't understand
Here is another way: - import random
-
-
f = open(r'path\dir\file_name', 'w')
-
f.writelines(['%.8f\n' % (random.random()) for _ in range(20)])
-
f.close()
Hey bvdet thanks for the tip I have managed to adjust it somewhat but I need to figure out how to get the output/data in to x and y columns here is my modified code. Hope you can help. -
# Random number generator
-
# This script generates random numbers
-
# The script then writes the result to a text file
-
# Guillermo Paniagua - 03/27/07
-
import random
-
-
-
-
f = open(r"grapes.txt", "w")
-
#text_file.write(i)
-
-
-
-
for i in range( 1, 601 ):
-
-
-
f.write( "%5f\n" % ( random.randrange( 1, 601 ) )),
-
-
#if numbers % 6 == 0:
-
#print
-
-
-
#text_file.writelines(numbers)
-
-
f.close()
-
-
raw_input("\n\nPress the enter key to exit.")
bvdet 2,851
Expert Mod 2GB
Hey bvdet thanks for the tip I have managed to adjust it somewhat but I need to figure out how to get the output/data in to x and y columns here is my modified code. Hope you can help. -
# Random number generator
-
# This script generates random numbers
-
# The script then writes the result to a text file
-
# Guillermo Paniagua - 03/27/07
-
import random
-
-
-
-
f = open(r"grapes.txt", "w")
-
#text_file.write(i)
-
-
-
-
for i in range( 1, 601 ):
-
-
-
f.write( "%5f\n" % ( random.randrange( 1, 601 ) )),
-
-
#if numbers % 6 == 0:
-
#print
-
-
-
#text_file.writelines(numbers)
-
-
f.close()
-
-
raw_input("\n\nPress the enter key to exit.")
What do you mean by "get the output/data in to x and y columns"?. Instead of each number on a separate line, do you want the numbers in pairs?
What do you mean by "get the output/data in to x and y columns"?. Instead of each number on a separate line, do you want the numbers in pairs?
hi there and yep an x and y column you are correct I do want numbers in pairs. :)
Please read "REPLY GUIDELINES" so that you know how to use CODE tags.
Then start using them. It's a real pain for others to try and see the structure of your code without them.
Thank you very much.
hi there and yep an x and y column you are correct I do want numbers in pairs. :)
it would also be good if the random floating point numbers where more than just always 12.00 , instead it would be nice to see 12.31,12.00, 14.56 etc... Do I have to change the datatype from float to decimal or something ? or use some type of math module ?
bvdet 2,851
Expert Mod 2GB
it would also be good if the random floating point numbers where more than just always 12.00 , instead it would be nice to see 12.31,12.00, 14.56 etc... Do I have to change the datatype from float to decimal or something ? or use some type of math module ?
Something like this? - import random
-
-
f = open(r'path\dir\file_name', 'w')
-
f.writelines(['%12.8f %14.8f\n' % (random.random()*random.randint(1,600), random.random()*random.randint(1,600)) for _ in range(20)])
-
f.close()
-
'''
-
178.09115903 133.36811062
-
60.54302702 93.40949828
-
245.20980645 254.00913526
-
350.37618749 49.87972752
-
276.50622882 248.12002096
-
234.91464718 3.01132671
-
0.10043225 379.17639623
-
21.99813057 213.64247640
-
5.09396543 2.42861560
-
101.41723993 32.51914585
-
29.31167588 430.25596881
-
48.70798269 90.25232557
-
33.68899482 153.09551692
-
107.74691175 108.88171789
-
2.27747461 13.77821158
-
125.42570521 53.47742587
-
90.04062953 38.57455190
-
171.75925277 234.66057796
-
2.05780089 234.13322485
-
303.65883903 20.71402190
-
'''
Yes that is right something like this... thanks so very much I think this will help alot. I will take a look at it and try to adapt it to see if it works. By the way Python is my new hobby I thought it would be cool to learn to program and thought I'd either learn using Python or IDL. So thanks alot looking over your suggestions has really helped me out :)
bvdet 2,851
Expert Mod 2GB
Yes that is right something like this... thanks so very much I think this will help alot. I will take a look at it and try to adapt it to see if it works. By the way Python is my new hobby I thought it would be cool to learn to program and thought I'd either learn using Python or IDL. So thanks alot looking over your suggestions has really helped me out :)
You are welcome. We are glad to help and appreciate the positive feedback. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JDJones |
last post by:
I'm trying to write a script that will read from a text list of songs
that I burned onto my CD (Albums011.txt), then write to the database
text the new text made ready for inserting into a...
|
by: Joan Bos |
last post by:
Hi,
Is there somewhere on the Internet a description of what a .NET
application config file should contain?
For our application, I have to write passwords encrypted in a config
file. The...
|
by: Aalok |
last post by:
This is what i want to do.
Read a text file as an input and based on that file, Create an outpu
text file which saves the contents of the input file in a specifi
format in the output file.
I...
|
by: RSB |
last post by:
Hi Every one,
i am trying to create a UserControl and i am passing a Array of strings to
it. Now based on the Array elements i am creating the LinkButtons
Dynamically. I am also passing a Event to...
|
by: Gan Quan |
last post by:
I'm writing a c++ program that has many (100+) threads read/write files
simultaneously. It works well if not considering the efficiency. The
file i/o seems to be the bottleneck.
This is my code...
|
by: kamill |
last post by:
i need to write content of one text file into another text file.
My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
|
by: Bill |
last post by:
Hello,
I'm trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone...
|
by: Paul David Buchan |
last post by:
Hello,
I'm attempting to write a program to read in database files (.dbf).
When I do it all as a single procedure in main, everything works.
However, what I really want, is to pass the database...
|
by: blunt |
last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: 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...
| |