# Random generator with constraints

16 views (last 30 days)
Kathryn Holder on 26 Feb 2021
Commented: Adam Danz on 26 Feb 2021
I am getting back into MATLAB and need to create a random generator that provides a matrix of 12 rows by 4 colums of numbers from (1,4), with non repeating numbers in row. And another separate random generator code that can provide a 1 row by 5 colums of numbers for (0,4), with non repeating numbers in row.

Adam Danz on 26 Feb 2021
> I am getting back into MATLAB and need to create a random generator that provides a matrix of 12 rows by 4 colums of numbers from (1,4), with non repeating numbers in row
sz = [12,4];
maxVal = 4;
M = cell2mat(arrayfun(@(i){randperm(maxVal,sz(2))},1:sz(1))');
>And another separate random generator code that can provide a 1 row by 5 colums of numbers for (0,4), with non repeating numbers in row.
V = randperm(5,5).'-1;
##### 2 CommentsShowHide 1 older comment
Adam Danz on 26 Feb 2021
Steven Lord's approach is simpler for the first part of the question.

Steven Lord on 26 Feb 2021
A = rand(6, 4);
[~, ind] = sort(A, 2)
ind = 6×4
4 1 2 3 4 2 3 1 1 2 3 4 4 2 1 3 2 4 3 1 1 3 2 4