How to generate uniformly distributed random integers?
4 ビュー (過去 30 日間)
古いコメントを表示
Pannir Selvam Elamvazhuthi
2011 年 7 月 25 日
編集済み: Sergio Enrique Pinto Castillo
2020 年 7 月 28 日
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?
0 件のコメント
採用された回答
the cyclist
2011 年 7 月 25 日
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.
1 件のコメント
Walter Roberson
2011 年 7 月 25 日
the cyclist is correct. Please see also the description in http://www.mathworks.com/matlabcentral/answers/12307-how-to-generate-a-uniform-random-variable-in-the-interval-1-1-with-mean-zero
その他の回答 (2 件)
Pannir Selvam Elamvazhuthi
2011 年 8 月 28 日
1 件のコメント
the cyclist
2011 年 8 月 29 日
No problem on the late response. However, you should consider deleting this "answer" (because it is not answer), and making it a comment on my answer.
Sergio Enrique Pinto Castillo
2020 年 7 月 28 日
編集済み: Sergio Enrique Pinto Castillo
2020 年 7 月 28 日
I think the answer is:
Num = 1000;
vec = randi([1 Num],1,Num);
This instruction generate a vector with random uniformly distributed integers between 1 and 1000
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!