How can i create a sequence that every symbol has the same probability as the others?
3 ビュー (過去 30 日間)
古いコメントを表示
How can i create a sequence that contains the symbols 0 , 1 , 2 , 3 with the same probability 1/4?
For example i created a sequence with 2 symbols 0 , 1 like this :
Seq = randn(1,N)<0.5 %N= length of the seq and each symbol has 1/2 probability
But i am confused about the 4 symbols any help?
0 件のコメント
採用された回答
John D'Errico
2020 年 6 月 14 日
編集済み: John D'Errico
2020 年 6 月 14 日
Actually, you are wrong in this. randn produces NORMALLY distibuted random numbers.
normcdf(0.5)
ans =
0.69146
So the sequence you generated has 1 appearing almost 70% of the time. Admittedly, sometimes I type randn when I intended to use rand.
N = 1e6;
Seq = randn(1,N) < 0.5;
hist(Seq)
If you wanted to use randn, you needed to compare the elements to ZERO, not 0.5.
Seq = randn(1,N)<0;
You could also have used rand that way, by comparing them to 0.5. Thus:
Seq = rand(1,N)<0.5;
A random sequence with 0,1,2,3 with equal probability is easy enough.
Seq = floor(rand(1,N)*4);
Or you could use randi.
randi([0,3],[1,N])
Finally, each of the above schemes will generate equi-probable sequences of elements, but there is no assurance you will get EXACTLY the same number of events in each bin. That only happens over the long term, where the events will tend to become approximately equal in count. In fact, it is unlikely that you will get EXACTLY equal numbers of events.
For example, given a sequence of 1000 events, what is the probability you would see EXACTLY 500 events in each bin from a true random sampling? This is something we can compute from a binomial distribution.
nchoosek(1000,500)/2^1000
ans =
0.025225
So only 2.5% of the time would we expect an exactly equal count of 500 heads and 500 tails from a fair coin tossed 1000 times.
Anyway, if that is what you want, then Image Analyst has shown you how to do that.
その他の回答 (1 件)
Image Analyst
2020 年 6 月 14 日
Try repelem() and randperm():
numPoints = 1000 % Multiple of 4
v = [0,1,2,3];
n = numPoints / length(v)
vec = repelem(v, n)
randomOrder = randperm(length(vec))
% Scramble them up.
vec = vec(randomOrder);
% Double check
for k = 1 : length(v)
count = sum(vec == v(k));
fprintf('%d shows up %d times.\n', v(k), count);
end
You'll see from the double check:
0 shows up 250 times.
1 shows up 250 times.
2 shows up 250 times.
3 shows up 250 times.
And the numbers will be randomly placed in the vector.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!