Re: randomly generate n of each of two types

2007-02-12 Thread Alan Isaac
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > >>> r = [x for x in test.random_types(10)] > >>> r > [False, False, False, False, False, False, False, False, False, False, > True, True, True, True, True, True, True, True, True, True] > > I think it needs a cast to a float: Mea culpa.

Re: randomly generate n of each of two types

2007-02-12 Thread [EMAIL PROTECTED]
> > This again has the "costs" I referred to: > creating a potentially large sequence, > and shuffling it. I thought I would see if I could do better, so I wrote this: import random def index_types(n, typelist=[True, False]): numtypes = len(typelist) total = float(n*numtypes) counts

Re: randomly generate n of each of two types

2007-02-11 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > Ah, I see what you mean... you're reminding me that the Original Poster > seems to want a biased set of almost-but-not-quite-randomly chosen > values, so that random_values(1) must return one each of True and False > and never True, True or False, False

Re: randomly generate n of each of two types

2007-02-11 Thread Steven D'Aprano
On Sun, 11 Feb 2007 22:20:24 -0800, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> 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

Re: randomly generate n of each of two types

2007-02-11 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> 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 equa

Re: randomly generate n of each of two types

2007-02-11 Thread Steven D'Aprano
On Mon, 12 Feb 2007 00:57:35 +, Alan Isaac wrote: > "Stargaming" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> ... types *= n >> ... shuffle(types) > > This again has the "costs" I referred to: > creating a potentially large sequence, > and shuffling it. (Additionally,

Re: randomly generate n of each of two types

2007-02-11 Thread Alan Isaac
"Stargaming" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > ... types *= n > ... shuffle(types) This again has the "costs" I referred to: creating a potentially large sequence, and shuffling it. (Additionally, shuffle cannot achieve many of the possible shuffles of a large list

Re: randomly generate n of each of two types

2007-02-11 Thread Stargaming
Alan Isaac schrieb: > I need access to 2*n random choices for two types > subject to a constraint that in the end I have > drawn n of each. I first tried:: > > def random_types(n,typelist=[True,False]): > types = typelist*n > random.shuffle(types) > for next_type in types: > y

Re: randomly generate n of each of two types

2007-02-11 Thread Paul Rubin
"Alan Isaac" <[EMAIL PROTECTED]> writes: > I need access to 2*n random choices for two types > subject to a constraint that in the end I have > drawn n of each. I first tried:: You mean you basically want to generate 2*n bools of which exactly half are True and half are False? Hmm (untested): f

randomly generate n of each of two types

2007-02-11 Thread Alan Isaac
I need access to 2*n random choices for two types subject to a constraint that in the end I have drawn n of each. I first tried:: def random_types(n,typelist=[True,False]): types = typelist*n random.shuffle(types) for next_type in types: yield next_type This works but has som