<[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.
>
> 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
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
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
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
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,
"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
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
"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
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
10 matches
Mail list logo