Hi,
How to generate 0 and 1 with equal probability?
I wanna generate sequence like 010101 or 011001 i.e probability of 0 and 1 should be 0.5.

댓글 수: 3

gkboy
gkboy 2018년 8월 7일
Hey,I am trying to get the random matrix with the elements only 1 and 0 where none of columns and rows can be 0 only and also the number of 0s and 1s should be in a percentage of 30% and 70%. have to use probability
Requirements 1 Random Matrix 2. elements only 1s and 0s 3. percentage of 0s: 1s is 3:7 4. None of the columns and rows can be with only 0 and only 1. have to have mix
James Tursa
James Tursa 2018년 8월 8일
This is sufficiently different from the original question that I would advise you delete this comment and instead post it as a new Question.
Srik G
Srik G 2022년 1월 27일
I wanna generate sequence like 010101 or 011001 with some sampling time in matlab- simulink blocks. Please suggest the block which can do this job in simulink ?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 9일

2 개 추천

round(rand)
or
rand >= 0.5

댓글 수: 4

Ronak Sakaria
Ronak Sakaria 2012년 5월 9일
well i want equal number of 1 and 0 to be generated in my answer. this syntax wouldnt do this.
Walter Roberson
Walter Roberson 2012년 5월 9일
T = [zeros(1,N), ones(1,N)];
T = T(randperm(2*N));
Warning: if you force the number of 1 and 0 to be equal, then that is _not_ a Uniform Random Distribution. The last bit could be completely predicted by knowing all the other bits.
Katyayani Modi
Katyayani Modi 2017년 9월 23일
He i have one question I want to generate 1's or 0's but 1's will occur 70% of the time with round, ceil, fix, floor function.
Image Analyst
Image Analyst 2017년 9월 23일
To get a specified percentage of 1's, try this:
numberOfElements = 1000; % Whatever.
percentageOfOnes = 70; % Whatever.
numberOfOnes = round(numberOfElements * percentageOfOnes / 100)
% Make initial signal with proper number of 0's and 1's.
signal = [ones(1, numberOfOnes), zeros(1, numberOfElements - numberOfOnes)];
% Scramble them up with randperm
signal = signal(randperm(length(signal)));
% Count them just to prove it
numOnes = sum(signal)

댓글을 달려면 로그인하십시오.

추가 답변 (8개)

Image Analyst
Image Analyst 2012년 5월 9일

1 개 추천

Ronak, This will do the trick:
n = 10; % Total length must be even to have even # of 0s and 1s.
numberOfOnes = n/2
% Get a list of random locations, with no number repeating.
indexes = randperm(n)
% Start off with all zeros.
x = zeros(1, n);
% Now make half of them, in random locations, a 1.
x(indexes(1:numberOfOnes)) = 1
Geoff
Geoff 2012년 5월 9일

0 개 추천

I wouldn't do this using the double version of rand. While it's not really incorrect to use rand for this, I would use randi instead. Integers are the native data type for random number generation.
This generates an m-by-n matrix of zeros and ones:
x = randi(2,m,n) - 1
There is still a chance, however, that when generating six random numbers they will be all 1, all 0, or anything else where the distribution is not perfect. If you require an equal number of ones and zeros, but scrambled, you could do this:
% By definition, we require an EVEN number for N
N = 6;
x = repmat([0 1], 1, N/2);
% Randomly switch two individual numbers N times.
for t = randi(N, 2, N)
x(t) = x(t([2 1]));
end
[edited: made swap code less C-like]

댓글 수: 5

Ronak Sakaria
Ronak Sakaria 2012년 5월 9일
thats the solution I'm looking for. Thanks Geoff
Geoff
Geoff 2012년 5월 9일
Oh, thanks Walter for pointing out the randperm() function. Didn't know that one. Ronak, to explain my loop (since you emailed me to ask about it):
I generate a 2xN array of indices and loop over it. Each iteration of the loop gives me a column-vector 't' containing two random indices. I then swap the values of 'x' at those two indices (even if they're the same). The x(t([2 1])) gives me the same two values of x but in reverse order (since I reversed the 't' vector).
Daniel Shub
Daniel Shub 2012년 5월 9일
I think randi uses the same random number generator as rand. As none of the random number generator give you logicals (what you want), I don't see why int8 is that much better than double.
Walter Roberson
Walter Roberson 2012년 5월 9일
Integers are (nearly always) what underlies uniform random generators. However, other distributions such as rand() may use other methods.
MATLAB does not expose the 32 bit integer generation capability of the Twister algorithm. If I recall, it uses two Twister calls to generate the 53 bit double in rand().
I do not have access to randi() to check, but because it needs to be uniform random over a variable range of integer values (rather than always over a power of 2), fairness considerations usually make it easier to use a double and multiply by the span.
Walter Roberson
Walter Roberson 2012년 5월 16일
I meant to write that randn() may use other methods.

댓글을 달려면 로그인하십시오.

Narayan Chaudhary
Narayan Chaudhary 2012년 5월 16일

0 개 추천

randerr(1,6,[0 3;0 1])
1x6 matrix with 0 and three non-zero entries with probability of 3 non zero entries being 1 i.e. 0,5 in overall matrix
Haoming Mai
Haoming Mai 2018년 2월 24일

0 개 추천

Hi
How to generate 0 and 1 with probability of 1/3?

댓글 수: 1

Image Analyst
Image Analyst 2018년 2월 24일
편집: Image Analyst 2018년 2월 24일
With randperm
total = 99
numOnes = round(total/3)
r = zeros(1, total); % All zeros to start.
% Now assign a third of them to 1
indexes = randperm(total, numOnes);
r(indexes) = 1
sum(r) % Check to make sure. Should be 33

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2018년 2월 24일
편집: James Tursa 2018년 8월 8일

0 개 추천

A few years after this question was posted, I posted
In which I proved that it is not possible to generate 0 and 1 with exactly equal probability with any of the MATLAB random number generators.
Akira Agata
Akira Agata 2020년 3월 18일

0 개 추천

Just FYI:
To evaluate BER (Bit Error Rate) of digital communication system, PRBS (Pseudo Random Binary Sequence) has been commonly used as a test pattern for many years. Since PRBS sequence with length of bits always contains specific number of 1s and 0s (more precisely, 1s and 0s ), maybe you can use this for your purpose.
% For example, the following code generates PRBS sequence of r=4 (15bit length)
pnSequence = comm.PNSequence(...
'Polynomial', [4 3 0],...
'InitialConditions',[0 0 0 1],...
'SamplesPerFrame', 15)
prbsSequence = pnSequence();
Prakash Reddy Pasham
Prakash Reddy Pasham 2022년 1월 3일

0 개 추천

I just need the 1D array of zeros and Ones with our required probability of ones.

댓글 수: 1

https://www.mathworks.com/matlabcentral/answers/37827-generate-0-and-1#comment_538887 shows how to achieve a specific density of ones -- for example, exactly 33 ones out of 99.
Note that this is not the same as the probability being 33/99 = 1/3 : probability is statistical. For example,
simulations = mean(rand(1000,1000) <= 1/3,1);
min(simulations), max(simulations)
ans = 0.2840
ans = 0.3870
histogram(simulations)

댓글을 달려면 로그인하십시오.

Omair Mohammad Ikram
Omair Mohammad Ikram 2022년 3월 22일
편집: Image Analyst 2022년 3월 22일

0 개 추천

What is the code for generation of a random sequence of bits with a specified probability of p0 of the bit 0?

댓글 수: 3

Image Analyst
Image Analyst 2022년 3월 22일
Try this:
p0 = 60; % Percent of zeros that we need in the final sequence.
numElements = 10000; % However many you want.
% Initialize a sequence of 1's.
sequence = ones(1, numElements);
numZeroes = round(numElements * p0 / 100);
% Get locations to randomly sprinkle the 0's into.
randomIndexes = randperm(numElements, numZeroes);
% Set those locations to 1.
sequence(randomIndexes) = 0;
% Verify
percentageOfOnes = 100 * sum(sequence) / numElements
percentageOfZeroes = 100 * sum(~sequence) / numElements
Adapt as needed.
Omair Mohammad Ikram
Omair Mohammad Ikram 2022년 3월 22일
Thanks alot
Walter Roberson
Walter Roberson 2022년 3월 22일
Do you need probability or do you need a specific number of values in a state? If you need exactly floor(p0*samples) then use randperm like above. If you want probability then
out = rand(1,samples) > p0;
for probability p0 of a 0.
More common would be to specify the probability of a 1, in which case you would typically use <.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by