I need to generate a certain number of random numbers starting from a sequence of integers and I use the following code: result<-sample(x=c(2:50), size=10e6, replace=T)
. I find that increasing the length of the result vector (up to a length of 10^6), the distribution of random numbers is not random if the length of the vector x
is an odd number. When plotting the histogram of result
I usually get that the 1st number of the sequence (in the example the '2') has a column (and so a number of elements) that is always higher than the other columns. If x=c(1:50)
, and so the length of x
is an even number, the behaviour of the random generator seems to be ok. Is there any issue about random number generators in R about this strange result? I use R 3.0.1 under Ubuntu 13.10.
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- why 48 bit seed in util Random class?
using table command you can see that there are of comparable frequency.
Notice that in breaks the values are 2 then 4 which means it includes frequencies of 2,3, and 4 in the first bin. the next bins all have only two values while the first one has 3 hence you see the spike in histogram plot.
As I mentioned in my comment above, this has absolutely nothing to do with random number generators.
Consider:
Something looks wrong, eh? But look closer:
versus...
Note that the first bin from
hist
appears to include all 2, 3 and 4 values. This is because the default binning strategy employed byhist
adds some "fuzziness" to the bin boundaries, which result in the first two break point being slightly less than 2.0 and slightly more than 4.0. Combine that with the intervals being right closed, and you get the resulting histogram.Compare with:
This may be an off topic tech-talk, but if you're dissatisfied with the default (Mersenne Twister) (pseudo)random number generator, try switching to another one, see
?RNGkind
. However, it is known that MT19937 passes many very rigid tests (like Marsaglia's Diehard test battery or TestU1), so there's most probably (EDIT: now: surely) something wrong with your code.Anyway, If your code had been ok (EDIT: again, we know it's not the case), what you would have obtained, is a nice procedure for testing the randomness of a RNG (in which MT does not perform well). Anyway, this may (at least theoretically) happen to be true, there's no perfect generator.
A nice introduction to the topic of random number generation and RNG testing is: Random Number Generation and Monte Carlo Methods by James E. Gentle.