How to generate 500 random string keys in matlab?

I need to understand on how to generate 500 random strings in matlab. and to generate hash values for the same.

Answers (1)

You can use the randi function to generate integers corresponding to the ascii values you want to allow in your random string.
An example to generate a character of length of 100 is as follows.
randomstr = char(randi([33 126],1,100));

6 Comments

There is a function DataHash available in Matlab Fileexchange that can help you calculate the hash values.
So using the randi() function, it certainly would obtain a string length of 100, but what if I need 500 random strings of length 100? Kindly let me know. I tried using a for but dint work.
char(randi([33 126],500,100))
Each row is one string. Simply access them using indexing, or split them up using num2cell.
As stephen suggested. there are two options. you can either convert to cellstr or string as follows.
cellarr = cellstr(char(randi([33 126],500,100)))
stringarr = string(char(randi([33 126],500,100)))
Hello thanks for the response, also how do we compute a hash function for string keys?
You pick a function that computes a hash (e.g. the one Mohammad linked), and you provide your char array as the input. Although since you are generating a random string anyway, why don't you generate a random array of 0-9A-F as a stand-in for your hash?
What are you trying to achieve?

Sign in to comment.

Categories

Asked:

on 4 Jun 2020

Commented:

Rik
on 9 Jun 2020

Community Treasure Hunt

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

Start Hunting!