필터 지우기
필터 지우기

Random number generation problem

조회 수: 1 (최근 30일)
Aftab Ahmed Khan
Aftab Ahmed Khan 2014년 9월 12일
댓글: Star Strider 2014년 9월 12일
Hello everyone, i am generating random channel assignment using randperm in the following code. Since the number of users are greater than the number of channels, so there will be a repetition of channel assignment. My question is how can i generate a random number from a given range [1-10], if the generated number for example is channel 9 and if that number 9 channel is unavailable then i want to generate another number between [1-10] but this time excluding number 9 channel. and this process continue until i get a free channel number from [1-10]
channels = 10;
users = 100;
challocation = ceil(channels*(randperm(users)/users));

채택된 답변

Star Strider
Star Strider 2014년 9월 12일
편집: Star Strider 2014년 9월 12일
Use the setdiff function to find the available channels, then randperm on the result.
  댓글 수: 6
Aftab Ahmed Khan
Aftab Ahmed Khan 2014년 9월 12일
Yes, it works exactly. Thanks dear.
Star Strider
Star Strider 2014년 9월 12일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 9월 12일
You say "the number of users are greater than the number of channels, so there will be a repetition of channel assignment" . In that case just use 'randperm' multiple times. Let n = number of channels and m = number of users.
q = ceil(m/n);
C = zeros(q*n,1);
for k = 0:q-1
C(k*n+1:k*n+n) = randperm(n);
end
C = C(1:m); % <-- These are the channels nos. assigned to the m users
The number of users assigned to any channel will never exceed the number of users assigned to another other channel by more than one and yet the assignments are random.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by