Random number generation problem

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));

 Accepted Answer

Star Strider
Star Strider on 12 Sep 2014
Edited: Star Strider on 12 Sep 2014
Use the setdiff function to find the available channels, then randperm on the result.

6 Comments

Hello Star, setdiff function is giving me the channels that are available but how can i randperm on the available channels now ? i didn't get that part....
The problem I discovered with randperm and its friends is that they produce a matrix of all possible permutations of their arguments. That works, but isn’t the most efficient.
For a first approximation, I would suggest this:
Channels = 1:10;
AssignChannel = randi(10);
AvailChan = setdiff(Channels,AssignChannel);
IdxRnd = randi([1 9]);
NextChan = AvailChan(IdxRnd);
That will produce the next randomly available channel. I’m not certain what you want to do beyond that.
Hello, I have done it like this, can you optimized these three lines for me as i don't want to have the same repetition of the condition with the while statement.
elseif chstate(1,chnum) == busy && ~isempty(chidle)
while chstate(1,chnum) == busy;
chnum = ceil(channels*rand);
end
I can’t comment on your code snippet, because I have no context for it.
I became interested enough in your problem to keep working on it in the interim. My code randomly assigns channels to unused channels, and also incorporates ‘DropChan’ that randomly frees up a channel and adds it to the available channels.
See if it does what you want:
Channels = 1:10;
AvailChan = Channels;
k1 = 1;
NextChan(k1) = randi(length(AvailChan));
while ~isempty(AvailChan)
if (randi(5) > 2) & (length(NextChan) > 1)
DropChan(k1) = NextChan(randi(length(NextChan)));
AvailChan = [AvailChan DropChan(k1)];
end
AvailChan = setdiff(AvailChan,NextChan(k1));
AvCh{k1} = AvailChan;
if ~isempty(AvailChan)
IdxRnd = randi([1 length(AvailChan)])
else
break
end
k1 = k1 + 1;
NextChan(k1) = AvailChan(IdxRnd);
end
The logic is that initially all channels are available, and the first channel assignment ‘NextChan’ occurs before the while loop. The loop keeps running as long as there are channels to assign (~isempty(AvailChan)). The first if block selects a condition to randomly drop a channel, as the channel user no longer needs it. (Change that condition as you wish, but keep the (length(NextChan) > 1) condition, since a channel cannot be dropped if there are no channels in use.) It then uses setdiff to find the available channels. If there are available channels (~isempty(AvailChan)), it chooses a random index ‘IdxRnd’ from that vector of indices, increments ‘k1’ and uses it and ‘IdxRnd’ to assign the next channel. The loop continues until there are no more channels to assign.
The ‘AvCh’ cell array keeps track of ‘AvailChan’ across the iterations. I put it in for my own diagnostic purposes, but it is not necessary in the code.
Yes, it works exactly. Thanks dear.
My pleasure!

Sign in to comment.

More Answers (1)

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.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!