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.

I am puzzled by the following behavior of RandomInteger.

Table[{k, Histogram[RandomInteger[k, 20000]]}, {k, 10, 100, 10}]

tables

It seems to me that n is less likely than other integers to be chosen as n increases beyond 40.

Can anybody explain what is happening?

(It is easy to work around this problem, but I am interested in why it occurs in the first place.)

share|improve this question

closed as off-topic by m_goldberg, bobthechemist, Michael E2, rasher, István Zachar Mar 4 at 11:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, bobthechemist, Michael E2, rasher, István Zachar
If this question can be reworded to fit the rules in the help center, please edit the question.

4  
It's just the histogram's default binning that isn't always divisible by the number of distinct values you have –  Rojo Mar 3 at 16:45
    
As @Rojo says it looks like a Histogram binning thing: Table[{k,ListPlot[Tally[RandomInteger[k,20000]],Filling-> Axis,PlotRange->{0,Automatic}]},{k,10,100,10}] –  chuy Mar 3 at 16:47
    
No: p = Table[RandomInteger[k, 100000], {k, 10, 100, 10}]; ListLinePlot[ Mean@Most[#/Last@#] &@Sort[Tally@#][[All, 2]] & /@ p, PlotRange -> {0, 1.2}] –  belisarius Mar 3 at 17:15
    
belisarius, Is your point that RandomInteger is not reasonably (pseudo)random throughout the range chosen? –  David Carraher Mar 3 at 17:39
    
No, I tried to show that the perceived effect is a histogram device. BTW ... If you don't use @userbame the other party doesn't get pinged, but I know you know –  belisarius Mar 3 at 18:23

1 Answer 1

up vote 6 down vote accepted

It's just the histogram's default binning that isn't always divisible by the number of distinct values you have

Try with hist = Histogram[#, Nearest[Divisors@Length@Union@#, 20]] &, but you will quite often get ugly (unaveraged or overaveraged) results, because more often than not there just isn't an exact divisor thats near a nice number of bins. The more you try to fix this problem, the more you'll probably understand why the automatic binning is as it is, and why manual specifications are allowed in the second argument.

share|improve this answer
2  
Thanks. You nailed it. –  David Carraher Mar 3 at 18:39
1  
@DavidCarraher You can use Tally in place of histogram (since you have integers) –  Szabolcs Mar 3 at 21:59

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