Matlab Random Word Generator
조회 수: 15 (최근 30일)
이전 댓글 표시
Hi All,
I have been attempting to generate a singular random word, from a set of words in a txt file labelled: dictionary.txt
So far, my code is as follows:
Dictionary="dictionary.txt";
Random=randperm(length("dictionary.txt"),1);
randword = Dictionary{Random}
However, when i run the code, it returns:
randword =
'dictionary.txt'
How can i make it so that the code displays a random word within the dictionary.txt file, rather than the file name?
Kind Regards,
Smoxk x
댓글 수: 0
채택된 답변
KSSV
2022년 4월 12일
Dictionary='dictionary.txt';
n = 5 ; % letters to pick from the string
Random=randperm(length(Dictionary),5);
randword = Dictionary(Random)
댓글 수: 7
KSSV
2022년 4월 13일
function OriginalWord = myfunction(filename,n)
% file name - nme of the file
% n - number of random letters needed from the file
import = importdata(file); % import the data
Num= numel(import); % get the length of the file
idx = randperm(Num, n); % get random number
OriginalWord=import(idx);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!