Random and whrandom

Fredrik Lundh fredrik at effbot.org
Wed Jan 24 09:49:14 EST 2001


Alex Martelli wrote:
> What is 'rand', I don't know: the Python docs don't mention it
> at all.  If it ever existed

It's an old emulation of C's "pick up troll or throw axe"-
style generator; here's the version from 1.4:

import whrandom

def srand(seed):
    whrandom.seed(seed%256, seed/256%256, seed/65536%256)

def rand():
    return int(whrandom.random() * 32768.0) % 32768

def choice(seq):
    return seq[rand() % len(seq)]

It disappeared somewhere on the way from 1.4 to 1.5.2.

Cheers /F





More information about the Python-list mailing list