Here's one to chuckle at later...
I was playing around with the random number generator, and wondering how random it is, so I decided to test it.
Made a list of ten zeros. Seeded random, created a loop, and incremented list[random.randint( 0,9)] each time.
Results were a little rough at 1000 and 10000, so I decided to try some bigger numbers (the more you flip a coin, the closer your results should approach 50%...).
Let's try 10**8.
Here's the good part. In typical BASIC-esque mentality, and remembering one of my Python books saying that 'for' is a little quicker than 'while', I used this:
I waited a couple minutes, as I thought I'd need to, when Windows tells me it needs to expand my page file (hmm, I just installed that memory optimizer...), and the program failed with a MemoryError.
I was chasing my tail, watching the memory graph eat 250+ free MB each time I started the program, and eliminating sections of code to find the culprit. I finally tried 'while' with a counter, which ran and finished without a blip on the graph.
Of course, while you chuckle, I now realize it wasn't the 'for', but the list with 10**8 items in it that caused my problem. :)
Anyway, so this isn't completely wasted bandwidth, I think I recall reading about an alternate version of range() that delivers each item just-in-time, rather than altogether at the beginning, but I couldn't find it in the Python docs. Does anyone know what that function might be?
Thanks.
I was playing around with the random number generator, and wondering how random it is, so I decided to test it.
Made a list of ten zeros. Seeded random, created a loop, and incremented list[random.randint( 0,9)] each time.
Results were a little rough at 1000 and 10000, so I decided to try some bigger numbers (the more you flip a coin, the closer your results should approach 50%...).
Let's try 10**8.
Here's the good part. In typical BASIC-esque mentality, and remembering one of my Python books saying that 'for' is a little quicker than 'while', I used this:
Code:
for i in range(10**8):
I was chasing my tail, watching the memory graph eat 250+ free MB each time I started the program, and eliminating sections of code to find the culprit. I finally tried 'while' with a counter, which ran and finished without a blip on the graph.
Of course, while you chuckle, I now realize it wasn't the 'for', but the list with 10**8 items in it that caused my problem. :)
Anyway, so this isn't completely wasted bandwidth, I think I recall reading about an alternate version of range() that delivers each item just-in-time, rather than altogether at the beginning, but I couldn't find it in the Python docs. Does anyone know what that function might be?
Thanks.
Comment