MATLAB: How to generate uniformly distributed random integers

random number generatoruniform

I've been using "ceil(4*rand)" to generate random integers from 1 to 4. But when I tried to check the uniformity, I found that I do not get a probability of 0.25 and instead have p(1)=0.24997, p(2)=0.25102, p(3)=0.24949 and p(4)=0.24951 after generating 10,00,000 values. Is it good enough to say that it's uniform?

Best Answer

Yes. You should not expect to get exactly 25% of each, every time. That is expected behavior of random numbers.
FYI, it may be more convenient for you to use the randi() function to do what you are doing.