R-How to generate random sample of a discrete random variables?
This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . Learn more . In R, I want to generate a random sample of a discrete random variable: X , where: P(X=a)=P(X=-a)=1/2 . I have been searching for a function online, but there seems no direct function doing this. I think you are looking to generate samples of a Bernoulli random variable. A Bernoulli random variable is a special case of a binomial random variable. Therefore, you can try rbinom(N,1,p) . This will generate N samples, with value 1 with probability p , value 0 with