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/… Commented 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. Commented Jul 1, 2013 at 17:28
  • Ok, thank you for your help. I will do that in future, regards. Commented 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()))
Sign up to request clarification or add additional context in comments.

2 Comments

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()
Could you tell me how to sort it by ascending number? I tried the sort() feature but no luck
0

Iterate over a sufficiently-large generator.

for linenum in xrange(500):
   ...

Comments

0

Surely we have simple way :)

from random import randint

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

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

Comments

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

Comments

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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.