[Edu-sig] re: Python as a first-year programming language
Terry Hancock
hancock@anansispaceworks.com
Mon, 28 Apr 2003 14:59:18 -0700
On Monday 28 April 2003 09:29 am, Kirby Urner wrote:
> On Monday 28 April 2003 01:37 am, Toby Donaldson wrote:
> Is there any way in Python to implement
non-deterministic
> > programming, e.g. for backtacking? It would be nice to
be able to write
> > something like this:
> >
> > def triple(a, b, c, n):
> > a = choice(n) # choice non-deterministically
chooses a number from 0
> > to n-1
> > b = choice(n)
> > c = choice(n)
> > if a**2 + b**2 == c**2:
> > return a, b, c
>
> You're asking if Python can do pseudo-random numbers?
Assuming that you *did* mean this, Toby, you should be aware
that your function can be written almost exactly as you
just did:
from random import choice
def triple(n):
a = choice(range(n))
b = choice(range(n))
c = choice(range(n))
if a**2 + b**2 == c**2:
return a, b, c
Try it. ;-)
>>> triple(3)
>>> triple(3)
(2, 0, 2)
>>> triple(3)
(1, 0, 1)
>>> triple(3)
>>> triple(3)
(0, 0, 0)
I have to admit that I don't understand why you want this
(I'm sure there's a good reason, and I'm hoping you'll tell
me so I'll know too ;-) ). Is this for some kind of
search efficiency reason? I have a feeling you're using
theory I never got because I never formally studied
programming.
Kirby's point of course (due to being a philosopher? ;-) )
is that such numbers aren't *truly* random. Which of course
they aren't. But they're "pretty random" -- without knowing
the application, I couldn't say if they were "random
enough". I have seen Monte Carlo simulation applications
which required *incredibly* well-distributed random numbers
with no cycles less than several billion (we were doing
forward-ray-tracing of billions of simulated photons to
build up enough to create an image -- you have to do this
in the forward direction rather than the usual reverse ray
tracing in some scientific applications). I was actually
surprised to find out that random number algorithms that
good exist!
Cheers,
Terry
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com