How to loop rand number without repition?

12 views (last 30 days)
Muhammad Sam'an
Muhammad Sam'an on 30 Sep 2020
Commented: Steven Lord on 1 Oct 2020
I Have
set phi = [ 1,2,3,4,5,6,7,8,9,10,11,12]
i want rand phi with looping with requirment for each iteration resulted single rand number without repitition?
How should i do ?

Answers (1)

madhan ravi
madhan ravi on 30 Sep 2020
doc randperm
  2 Comments
Muhammad Sam'an
Muhammad Sam'an on 30 Sep 2020
thank you, but that i need single number rand
my listing math lab code
clc;clear;close all;
c=[10 2 20 11
12 7 9 20
4 14 16 18];
s=[15
25
10
];
d=[5 15 15 15];
[m,n]=size(c);
x=zeros(m,n);
phi=1:m*n
for i=1:m*n
k1 = randperm(length(phi))
ii=[1+mod((k1-1),m)]
j=[1+mod((k1-1),n)]
x(ii,j)=min(s(ii),d(j))
s(ii)= s(ii)-x(ii,j)
d(j)= d(j)-x(ii,j)
end
Steven Lord
Steven Lord on 1 Oct 2020
"Shuffle" the vector before you enter the loop. Inside the loop draw the next element of the vector that you haven't used yet.
r = randperm(52);
for k = 1:5
fprintf("Drawing card %d.\n", r(k))
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!