[Tutor] A file containing a string of 1 billion random digits.
Richard D. Moores
rdmoores at gmail.com
Mon Jul 19 14:29:53 CEST 2010
On Mon, Jul 19, 2010 at 04:51, Peter Otten <__peter__ at web.de> wrote:
> bob gailer wrote:
>
>> Check this out:
>>
>> import random, time
>> s = time.time()
>> cycles = 1000
>> d = "0123456789"*100
>> f = open("numbers.txt", "w")
>> for i in xrange(n):
>> l = []
>> l.extend(random.sample(d, 1000))
>> f.write(''.join(l))
>> f.close()
>> print time.time() - s
>
> Note that this is not random. E. g. the start sequence "0"*101 should have a
> likelyhood of 1/10**101 but is impossible to generate with your setup.
I not sure exactly what you mean, because I don't fully understand
that '*' (despite Alan's patient explanation), but if you run
import random
cycles = 100000
d = "0123456789"*10
for i in range(cycles):
l = []
l.extend(random.sample(d, 100))
s = (''.join(l))
if s[:4] == '0101':
print(s)
You'll see a bunch of strings that begin with "0101"
Or if you run
import random
cycles = 50
d = "0123456789"*10
for i in range(cycles):
l = []
l.extend(random.sample(d, 100))
s = (''.join(l))
if s[:1] == '0':
print(s)
You'll see some that begin with '0'.
Am I on the right track?
Dick
More information about the Tutor
mailing list