How to do Randi with letters
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Shahar ben ezra
el 27 de Abr. de 2021
Comentada: Shahar ben ezra
el 28 de Abr. de 2021
Is it possible to do the RANDI function with letters?
And you will see an example like this:
X7S7-6S6A-Y555-U75T
for i=1:4
r(i)= randi([1000,9999],1,1);
end
r= num2str(r);
fprintf('Your code is -"%s"',r)
the print look like : Your code is -"4818 3432 2773 8395"
tnx :)
0 comentarios
Respuesta aceptada
Jan
el 27 de Abr. de 2021
Editada: Jan
el 27 de Abr. de 2021
Provide a pool with valid characters and use randi() to determine the random indices:
Pool = ['A':'Z', '0':'9'];
Key = Pool(randi(numel(Pool), 1, 16));
KeyC = cellstr(reshape(Key, 4, []).');
KeyS = strjoin(KeyC, '-');
fprintf('Your code is: %s', KeyS)
% Or after creation of [Key]:
KeyS = sprintf('%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c', Key); % ugly
Más respuestas (1)
Steven Lord
el 27 de Abr. de 2021
There's no need for the first loop since randi can create a vector of values.
r = randi([1000,9999],1,4)
Since you're using a release that includes string I'd use that.
S = string(r)
Now you can join the strings in S together.
code = "Your code is - " + join(S, " ")
There are other ways you can "do randi with letters" particularly for your first example where you wanted something like "X7S7-6S6A-Y555-U75T"
allowedChars = ['A':'Z' '0':'9'];
whichChars = randi(numel(allowedChars), [4 4]);
selectedChars = allowedChars(whichChars)
Now turn each of those vectors into strings and join them as before.
code = "Your code is - " + join(string(selectedChars), " : ")
Ver también
Categorías
Más información sobre STMicroelectronics Discovery Boards 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!