Generating unique random numbers less than 1 in matrix

15 views (last 30 days)
How can I generate unique random numbers less than 1 in matrix?
  1 Comment
Adam on 12 Sep 2017
doc unique
will ensure uniqueness. If you happen to have a repeated number then just generate a new one to replace it.

Sign in to comment.

Answers (3)

KSSV on 12 Sep 2017
doc rand
A = rand
B = rand(3)
C = rand(1,10)
D = rand(10,1)
José-Luis on 12 Sep 2017
Edited: José-Luis on 12 Sep 2017
Random numbers can be repeated. The one has nothing to do with the other.
[0.5 0.5 0.5 0.5 0.5 0.5 0.5] is a random sequence, as likely as any other combination you select if you are using a uniform distribution. Ok, you need to include the independent and identically distributed caveat, but still.
People get hit by lightning. People win the lottery.
You can apply unique() to ensure that no numbers are repeated.

Sign in to comment.

Jan on 14 Sep 2017
Edited: Jan on 14 Sep 2017
"Unique" and "random" is a contradiction. But you can choose a unique set of numbers easily:
ready = false;
n = 10;
while ~ready
data = rand(1, n);
ready = (numel(unique(data)) == n);
ready = all(diff(sort(data)));
This will most likely find a unique set inthe first iteration, because the chance that the two numbers replied by rand(1,2) are equal is 2^-52.

John BG
John BG on 12 Sep 2017
Edited: John BG on 12 Sep 2017
Hi Hany Salem
Initial consideration
The 'shoot first and check later' approach works fairly, I mean the 'a posteriori' application of unique, as long as the size of the generated pool is small enough for the calculations.
As soon as the pool size goes above certain size, depending upon the type of calculation used to generate the random numbers, the application of 'unique' AFTER the random generation is going to gradually increase the delay to tell whether the number is already in the pool or not.
It's like in the shooting range, if targets are close you can shoot first and then look, the amount of repetitions is going to be fairly low as long as the target is close enough.
But the longer the target is placed, the higher the amount of repetitions will be required.
The gradual increase of the pool size and 'check later' approach implies that there is going to be a range beyond which hitting the target is going to take so long that might even get to Inf, meaning, better get the target closer, or go for a break.
I am explaining all this because I had a discussion earlier on regarding how to generate 2D random points within a rectangle, for a mobile communications LTE application, and the question originator agreed with my answer regarding that a solution EXCLUDING points is inherently faster, doesn't limit the pool size with time, and doesn't need to check after generating.
Agreeing with Jose Luis, the following link is the QA that has a to do with the right approach for this question
So, Hany Salem
you can simply
ans =
format long
ans =
this is ok as long as the lengths of the generated numbers can be contained within the default number classes.
If you don't want to use int or double or vpa there's been now for a while an effective approach to variable length arithmetic: use characters as numbers
again, while
ans =
as soon as
A =
ans =
and in the Workspace one notices that the answer has only a fraction of the sought number, which is actually the value assigned to the result, losing decimals
ans =
a start point would be
for k=1:1:N
A=[A num2str(randi([0 9],1,1))];
now despite in the workspace L shows 0.5875
When checking
L =
And when N exceeding the size of a built-in numeric class, you still have the number in character format.
Then, depending on what is the purpose of the generated numbers, you may want to define operations using the numbers contained in strings.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

Community Treasure Hunt

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

Start Hunting!