# Im using randsample() but im getting an error

17 views (last 30 days)
Tariq Hammoudeh on 6 Jan 2022
Commented: Walter Roberson on 7 Jan 2022
Im using randsample to make the program choose a random element from an array,
A=[2 3 4 5];
random=randsample(A,1)
but im getting an error that says 'randsample' requires Statistics and Machine Learning Toolbox.
So how can i make it work, or is there any other way i can make a random selection.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Geoff Hayes on 7 Jan 2022
@Tariq Hammoudeh yes, randsample requires the Statistics and Machine Learning Toolbox. If you just want to choose one random element from this array, you could do something like
A = [2 3 4 5];
randomValue = A(randi(length(A),1));
##### 6 CommentsShowHide 5 older comments
Walter Roberson on 7 Jan 2022
I would say... don't do that.
one=readmtarix();
two=readmatrix();
three=readmatrix();
A = {one two three};
x = randi(length(A));
board_configuration = A{x};
if x == 1
...
elseif x == 2
...
end

Sign in to comment.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by