Matlab Random Word Generator

15 visualizaciones (últimos 30 días)
Smoxk x
Smoxk x el 12 de Abr. de 2022
Comentada: KSSV el 13 de Abr. de 2022
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

Respuesta aceptada

KSSV
KSSV el 12 de Abr. de 2022
Dictionary='dictionary.txt';
n = 5 ; % letters to pick from the string
Random=randperm(length(Dictionary),5);
randword = Dictionary(Random)
randword = 'ynx.o'
  7 comentarios
Smoxk x
Smoxk x el 12 de Abr. de 2022
Editada: Smoxk x el 12 de Abr. de 2022
Hey @KSSV,
How would i go about making the below code into a function?
file = 'dictionary.txt'; % file name
import = importdata(file); % import the data
Num= numel(import); % get the length of the file
idx = randperm(Num, 1); % get random number
OriginalWord=import(idx);
KSSV
KSSV el 13 de Abr. de 2022
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by