[Tutor] Workaround for limitation in xrange()?

Dick Moores rdm at rcblue.com
Tue Oct 10 22:01:24 CEST 2006


At 11:34 AM 10/10/2006, Kent Johnson wrote:
>Dick Moores wrote:
>>Here are the suggestions I've received:
><snipped>
>
>>All 3 are essentially the same, aren't they. 
>>Which makes me feel even dumber, because I 
>>don't understand any of them. I've consulted 3 
>>books, and still don't understand the use of yield.
>
>Yes, they are pretty much the same. Mine is 
>bare-bones, just doing what you specifically 
>asked for. Danny's is a full replacement for xrange(); Andrei's is in between.
>
>Here is a good introduction to generators:
>http://www.python.org/doc/2.2.3/whatsnew/node5.html

While I'm studying that, could you modify my 
while loop so that it incorporates your 
suggestion in a way that improves it? That would 
be a big help to me in understanding generators 
and the use of yield, I believe.

I don't want to give up on generators, even 
though your referenced article grants me 
permission to do so: "Generators are considered a 
new part of the core Python language, but 
learning or using them isn't compulsory; if they 
don't solve any problems that you have, feel free to ignore them."

>>I've realized now that all 3 scripts require an 
>>immense number of calls to random()
>
>Um, yeah, more than 2**31-1, right? :-)

At least several trillions, I'm thinking now, naively.

>By the way your approach to testing the 
>'goodness' of random() is pretty naive. For 
>example a random() that just repeats 0...99 over 
>and over would score very well. For large repeat 
>counts it could even repeat the same number for 
>a while and score well: 1111111112222222233333333 etc.

Sure, but passing the 3 tests I had in mind would 
be necessary if not sufficient conditions. 
Obviously random() would not be of the sorts you 
mention. Functions like those wouldn't be chosen for use in Python, right?

>  You might be interested in this article and the links at the bottom:
>http://en.wikipedia.org/wiki/Pseudo-random_number_generator

I'll be sure to check this out. And a friend 
tells me he has the Knuth vol. 2 (with that 
"Chapter 3, pp.1–193. Extensive coverage of 
statistical tests for non-randomness).

Dick





More information about the Tutor mailing list