Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Screen Shot of my Mathematica Commands

I was using RandomSample with very skewed weights. I am asking for a random sample of 6 numbers 1 through 100 without replacement. In the screen shown, the weights for 1 through 6 are 100 000, with all others less then 1; hence, the RandomSample should return numbers 1 through 6 is a random order very very often. But it does not.

I have copied and pasted the commands shown.

Am I missing something? Or am I misunderstanding the RandomSample function? Or is this actually a bug?

share|improve this question
7  
Please do not post images as code. Read the formatting FAQ in the Help section, and post actual code. –  rasher Mar 5 at 3:03

1 Answer 1

Definitely something interesting going on here.

c[x_] := Count[Table[RandomSample[{100000, 0, 1} -> {1, 2, 3}, 1], {x}], {3}]

DiscretePlot[c[x]/x, {x, 10, 400}]

enter image description here

Note that changing the zero weight to something greater than zero seems to give the expected behavior. Some experimentation seems to suggest that adding a zero weight in the presence of weights with large difference in scale causes other weights to be treated as equal.

I also notice that even when the weights are in the same ballpark odd stuff happens when we cross the 249 to 250 replication mark.

c2[x_] := Count[Table[RandomSample[{2, 0, 1, 2} -> {1, 2, 3, 4}, 1], {x}], {3}]
DiscretePlot[c[x]/x, {x, 10, 400}]

enter image description here

share|improve this answer
1  
It is clearly documented "The weights must be positive...", emphasis mine. –  rasher Mar 5 at 3:35
4  
The threshold of 250 is likely due to auto-compilation in Table (which kicks in at precisely 250). –  Szabolcs Mar 5 at 3:41
1  
This would explain why weights can't be zero, but if it's really so by design, RandomSample should give a warning, the same as it warns about negative weights. –  Szabolcs Mar 5 at 3:48
1  
The weird behaviour at 250 is only with RandomSample, not RandomChoice, and only with integer weights, not with floating point weights. –  Szabolcs Mar 5 at 3:50
    
@Szabolcs I suspected auto-compilation as well. –  Andy Ross Mar 5 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.