[Date Index]
[Thread Index]
[Author Index]
Re: RandomChoice does not accept null weights
*To*: mathgroup at smc.vnet.net
*Subject*: [mg83411] Re: RandomChoice does not accept null weights
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Tue, 20 Nov 2007 03:42:40 -0500 (EST)
*Organization*: The Open University, Milton Keynes, UK
*References*: <fhrs7v$5pe$1@smc.vnet.net>
Szabolcs Horv=C3=A1t wrote:
> RandomChoice does not accept 0 weights.
>
> Example:
>
> In[1]:= RandomChoice[{0,1,2}->{a,b,c}]
>
> During evaluation of In[1]:= RandomChoice::weightv: The weights given=
on
> the left-hand side of {0,1,2}->{a,b,c} should be a list of positive
> numerical quantities having the same length as the list given on the
> right-hand side. >>
>
> Out[1]= RandomChoice[{0,1,2}->{a,b,c}]
>
> Is this the intended behaviour?
As far as I am aware of, the help page for RandomChoice does not state
or imply that the weights must be positive. OTOH, the above error
message clearly claims that the weight must be positive values. Shall we =
conclude that the documentation is again flawed or incomplete or
misleading or ... (who knows what!) and that the behavior of
RandomChoice is at it is by design?
> Of course it is very easy to work around this problem by either adding =
a
> very small number to all weights, or Pick[]ing out those elements whose=
> weight is not 0 before passing them to RandomChoice[].
>
> But in many situations it would be very useful if RandomChoice accepted=
> 0 as a weight ...
I guess you have in mind something similar to the following function.
In[1]:= Clear[w0RandomChoice]
w0RandomChoice[
wght_List /; VectorQ[wght, NonNegative] -> val_?VectorQ] /;
Length[wght] == Length[val] :=
RandomChoice[
Rule[Select[wght, Positive], Pick[val, Unitize@wght, 1]]]
w0RandomChoice[wght_List /; VectorQ[wght, NonNegative] -> val_List,
n_Integer /; Positive[n]] /; Length[wght] == Length[val] :=
RandomChoice[
Rule[Select[wght, Positive], Pick[val, Unitize@wght, 1]], n]
In[4]:= w0RandomChoice[{0, 1, 2} -> {a, b, c}]
Out[4]= b
In[5]:= w0RandomChoice[{0, 1, 2} -> {a, b, c}, 1]
Out[5]= {b}
In[6]:= w0RandomChoice[{0, 1, 2} -> {a, b, c}, 10]
Out[6]= {c, c, c, b, c, b, c, c, c, c}
In[7]:= w0RandomChoice[{0, 1, 2} -> {a, b, c}, -10]
Out[7]= w0RandomChoice[{0, 1, 2} -> {a, b, c}, -10]
In[8]:= w0RandomChoice[{0, 1, 2} -> {a, b, c}, 3.]
Out[8]= w0RandomChoice[{0, 1, 2} -> {a, b, c}, 3.]
In[9]:= w0RandomChoice[{0, 1, -2} -> {a, b, c}, 3]
Out[9]= w0RandomChoice[{0, 1, -2} -> {a, b, c}, 3]
In[10]:= w0RandomChoice[{1, 2} -> {a, b, c}, 3]
Out[10]= w0RandomChoice[{1, 2} -> {a, b, c}, 3]
In[11]:= w0RandomChoice[{.0, .1, .2} -> {a, b, c}, 3]
Out[11]= {c, c, c}
Regards,
--
Jean-Marc
Prev by Date:
**Re: memory release problem in mathematica6.0**
Next by Date:
**Re: trying to Import[] USGS-resident DEM data**
Previous by thread:
**Re: RandomChoice does not accept null weights**
Next by thread:
**Re: RandomChoice does not accept null weights**
| |