Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread dickinsm
Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > > [...] Here are three recipes, each more pedantic than the last. They all assume that Python floats are IEE

Conflicting needs for __init__ method

2007-01-14 Thread dickinsm
Here's an example of a problem that I've recently come up against for the umpteenth time. It's not difficult to solve, but my previous solutions have never seemed quite right, so I'm writing to ask whether others have encountered this problem, and if so what solutions they've come up with. Suppos

Re: Mirror imaging binary numbers

2006-12-06 Thread dickinsm
On Dec 6, 7:20 pm, Grant Edwards <[EMAIL PROTECTED]> wrote: > It's a little less obtuse if you spell it this way: > > def flipbits(x): > """reverse bits in a byte""" > x1 = x << 4 | x >> 4 > x2 = (x1 & 0x33) << 2 | (x1 & 0xcc) >> 2 > return (x2 & 0x55) << 1 | (x2 & 0xaa) >> 1 > G

Re: Mirror imaging binary numbers

2006-12-06 Thread dickinsm
On Dec 6, 6:01 pm, "Craig" <[EMAIL PROTECTED]> wrote: > Thanks so much for the response. I have an array of individual bytes > which will eventually make up a binary bitmap image that is loaded onto > an LCD screen (1 = black dot, 0 = white dot). At the moment each byte > is reversed to what it s

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread dickinsm
On Nov 29, 6:59 pm, "Steve Bergman" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > > BTW, can this code be made any more efficient? > > > I'm not sure, but the following code takes around 6 seconds on my > > 1.2Ghz iBook. How does it run on your machine? > > Hmm. Come to think of it,

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread dickinsm
> BTW, can this code be made any more efficient? I'm not sure, but the following code takes around 6 seconds on my 1.2Ghz iBook. How does it run on your machine? def smallPrimes(n): """Given an integer n, compute a list of the primes < n""" if n <= 2: return [] sieve = range

Re: Shortest prime number program

2006-02-11 Thread dickinsm
swisscheese wrote: > > r=range(2,99) > m=[x*y for x in r for y in r] > [x for x in r if not x in m] How about: [2]+[x for x in range(1,99) if 2**x%x==2] Mark -- http://mail.python.org/mailman/listinfo/python-list