[Tutor] A file containing a string of 1 billion random digits.
Richard D. Moores
rdmoores at gmail.com
Sat Jul 17 23:48:06 CEST 2010
On Sat, Jul 17, 2010 at 13:56, Dave Angel <davea at ieee.org> wrote:
> Your code is both far more complex than it need be, and inaccurate in the
> stated goal of producing random digits.
Dave, please see the code I posted in this thread more recently,
<http://tutoree7.pastebin.com/M1U00vtD>.
> There's no need to try to have all the "digits" in memory at the same time.
> As others have pointed out, since you're using write() method, there's no
> newline automatically added, so there's no problem in writing a few bytes at
> a time.
I write a 1000 at a time.
> Your concern over a leading zero applies equally to two leading zeroes, or
> three, or whatever.
I don't think so. Look again at my function, randIntOfGivenLength().
Try a for loop with it, say with randIntOfGivenLength(9) and looping
100 times:
for x in range(100):
n = randIntOfGivenLength(9)
n = str(n)
print(n)
You'll see that all strings have 9 digits. The problem is that none of
them begin with "0". These are just strings of almost-random digits,
and in the script I give the 1st digit a chance to be a "0" by
prepending a digit -- just one -- chosen by random.choice() from the
string "0123456789". There's no need for zero-filling. I do agree that
it would be good to eliminate the need to use str(). I'll look at the
first part of your formatting suggestion, and skip the zero-filling.
> So instead of using str() to convert an int to a
> string, use string formatting. Either the % operator or the format()
> method. Just use a format that guarantees zero-filling to the same width
> field as the size limit you supplied.
>
> So generate a number between 0 and 9999999999,
That would be between 1,000,000,000 and 9,999,999,999 .
Dick
> or whatever, and convert that
> to a fixed width string possibly containing leading zeros. Write the string
> out, and loop back around.
>
> DaveA
More information about the Tutor
mailing list