Generating long string with 0 (probability of 0.5) and 1 (probability of 0.5) with space in between two adjacent characters

1 view (last 30 days)
I need to generate a long string containing characters of only 0 and 1. Each charater occurs with probability of 0.5. Besides, all the 0/1 characters need to be separated with a space.
Is there an easy way to do this?
Thanks in adbance!

Accepted Answer

Star Strider
Star Strider on 2 Sep 2019
One approach:
N = 25; % String Length
v = sprintf('%d ', randi([0 1], 1, N))

More Answers (1)

Guillaume
Guillaume on 2 Sep 2019
One way:
num01 = 1e5; %how many 0 and 1 you want in total
strjoin(string(randi([0 1], 1, num01)), " ")

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!