randomly generate n of each of two types

Paul Rubin http
Mon Feb 12 01:20:24 EST 2007


Steven D'Aprano <steve at REMOVEME.cybersource.com.au> writes:
> If you want to avoid shuffle, here's an alternative:
> 
> def random_values(n, valuelist=[True, False]):
>     N = len(valuelist)
>     for _ in range(N*n):
>          yield valuelist[random.randrange(0, N)]

That is not guaranteed to yield exactly equal numbers of true and false.



More information about the Python-list mailing list