[Edu-sig] Programming for the fun of it
Kirby Urner
pdx4d@teleport.com
Tue, 12 Dec 2000 17:31:21 -0800
Jeff Elkner wrote:
> #!/usr/bin/python
> import time
> import random
>
> notes = ['C', 'D', 'E', 'F', 'G']
>
> while 1:
> print "Now play " + notes[random.randint(0, len(notes) - 1)] + " . .
>. ."
> time.sleep(2)
Just wanted to remind you about the random.choice() method, which
picks from a list directly, allowing you to bypass the indexing
syntax e.g.
>>> import time,random
>>> def play()
notes = ['C', 'D', 'E', 'F', 'G']
while 1:
print "Now play " + random.choice(notes)+ " . . . . "
time.sleep(2)
>>> play()
Now play C . .
Now play G . .
Now play E . .
etc.
On the random.randint() front, I've been playing around with
cryptography, implemented a slow (put pretty) Blowfish, and
now I'm looking at RSA. That starts with the requirement for
2 100-digit prime numbers (shall we say). So what's a quick
and dirty way to get some random 100-digit primes (allowing
pseudorandom digit-picking for now)?
Step one is to get any 100-digit number at all. If it's even
then add 1, or if odd and easily divisible by a low prime, then
add 2, and keep going until your prime tests (whatever they
might be) certify the primehood of your candidate.
So let's do step one this way:
>>> def mkrandint(digits):
bignum = ""
for i in range(digits):
bignum = bignum + str(random.randint(0,9))
return long(bignum)
>>> mkrandint(100)
7963087367299610171220441460319216044629224943047806172386921
203875082185538604305205721605709979869L
That's a big number all right. Dunno if it's prime (probably
not) -- I don't have the tests for that written. A Fermat test
seems impractical. Has anyone done Miller-Rabin in pure Python,
for pedagogic purposes? Still looking into it. Yes, I know,
I know, I need to join the Python cryptography SIG.
Kirby