Random generator with constraints

1 view (last 30 days)
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.

Accepted Answer

Adam Danz
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 Comments
Adam Danz
Adam Danz on 26 Feb 2021
Steven Lord's approach is simpler for the first part of the question.

Sign in to comment.

More Answers (1)

Steven Lord
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

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!