1

I have created a set of 6 random integers and I wish to write 500 of them into a text file so it looks like this inside the text file:

x, x, xx, x, xx, x \n x, x, x, xx, x, x ....etc

(where x is an integer)

from random import shuffle, randint

def rn():
    return randint(1,49);

print "Random numbers are: " , rn(), rn(), rn(), rn(), rn(), rn()

There must be an easier way than pasting the last line 500 times?

EDIT: Why all the down votes? I'm sorry if this is a basic question for you guys but for someone learning python it's not.

3
  • 1
    Regarding the downvotes, I'd image people will recommend you complete a basic Python tutorial first: codecademy.com/courses/… Jul 1, 2013 at 17:28
  • 2
    Downvoted because solutions to your problem (e.g. Loops) are found in the most basic of programming tutorials; and this site is not meant to teach you the fundamentals of programming. It is expected that you do some work on your own (like reading a python tutorial) before asking a question.
    – l4mpi
    Jul 1, 2013 at 17:28
  • Ok, thank you for your help. I will do that in future, regards. Jul 1, 2013 at 17:38

5 Answers 5

3

How about this:

print "Random numbers are: "
for _ in xrange(500):
    print rn(), rn(), rn(), rn(), rn(), rn()

If you want to write to text file:

with open('Output.txt', 'w') as f:
    f.write("Random numbers are: \n")
    for _ in xrange(500):
        f.write("%s,%s,%s,%s,%s,%s\n" % (rn(), rn(), rn(), rn(), rn(), rn()))
2
  • That's great, but I want to print them into a text file, how would I do that? e.g ext_file = open("Output.txt", "w") text_file.write("Purchase Amount: " 'rn(), rn(), rn(), rn(), rn(), rn()') text_file.close() Jul 1, 2013 at 17:09
  • Could you tell me how to sort it by ascending number? I tried the sort() feature but no luck Jul 1, 2013 at 17:52
0

Iterate over a sufficiently-large generator.

for linenum in xrange(500):
   ...
0

Surely we have simple way :)

from random import randint

def rn():
    return randint(1, 49)

for i in xrange(500):
    print rn()
0

Could use the following:

from random import randint
from itertools import islice

rand_ints = iter(lambda: str(randint(1, 49)), '')
print 'Random numbers are: ' + ' '.join(islice(rand_ints, 500))

And dump those to a file as such:

with open('output', 'w') as fout:
    for i in xrange(500): # do 500 rows
        print >> fout, 'Random numbers are: ' + ' '.join(islice(rand_ints, 6)) # of 6 each
0

Use a for-loop:

from random import shuffle, randint

def rn():
    return randint(1,49);

with open('out.txt', 'w') as f:
    for _ in xrange(500):
        f.write(str(rn()) + '\n')

If you want 6 of them on each line:

with open('out.txt', 'w') as f:
    for _ in xrange(500):
        strs = "Purchase Amount: {}\n".format(" ".join(str(rn()) 
                                                          for _ in xrange(6)))
        f.write(strs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.