How can i create a sequence that differs from another one with some probability p?

Viewed 39

Hello i have a random sequence of 10 bits x=0100100011

I want to create another sequence y but with probability 0.2 is different than x.That actually means that when i have 10 bits i approximately expect that 2 out 10 bits are different than my initial sequence. How can implement that on matlab?

Thank you.

2 Answers

Assuming that

  • each bit has the same probability of being flipped, and
  • flips are independient for different bit positions,

you can do it as follows:

x = [0 1 0 0 1 0 0 0 1 1];
p = 0.2; % probability of bit flip
y = xor(x, rand(size(x))<p);

On average, x and y will differ in a fraction p of the bits.

How it works

rand(size(x))<p creates a sequence the same size as x, where each element contains 1 with probability p and 0 otherwise. Each 1 indicates that the corresponding value in x should be changed. This is done with xor(x, ...).

While in the question it is expected that approximately 2 out of 10 bits to be different than the initial sequence, in the following answer exactly 2 out of 10 bits are different than the initial sequence:

x = [0 1 0 0 1 0 0 0 1 1];
n = 10;
p = 2;

pos = randperm(n, p);
x(pos) = ~x(pos);
Related