MATLAB: How to generate a sequence of words

sequence

Hello, I am trying to create a program that generates the word "to" and determines how many repetitions it takes to reach said word. I had no problem generating the letter "t", but I became stuck once I had to generate a letter adjacent to another, in order to create "to". Any help would be appreciated. Here is my code to generate the letter "t":
Letters = 'abcdefghijklmnopqrstuvwxyz_';
% By using a random integer range from 1 to 27 we can randomly select letters.
MinInt = 1; MaxInt = 27;
% create a vector of random integers from 1 to 27
LetterPositions = floor((MaxInt - MinInt + 1)*rand(1,27) + MinInt);
% create a vector of random letters A to Z
RandomLetters = Letters(LetterPositions)';
fprintf('The attempted sequence is t\n')
tic
fprintf('It took %g attempts to create the sequence:t\n',sum(RandomLetters == 't'))
toc
fprintf('The attempted sequence is to\n')
tic
% here is where I'm stuck

Best Answer

You can improve your code
Letters = ['a':'z' '_']
LetterPositions =randi(27,1,27)
RandomLetters = Letters(LetterPositions)'
%If you want to generate two letters
idx=randi(27,1,2)
Letters(idx)