Generating unique random numbers less than 1 in matrix
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How can I generate unique random numbers less than 1 in matrix?
1 commentaire
Adam
le 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.
Réponses (3)
KSSV
le 12 Sep 2017
doc rand
A = rand
B = rand(3)
C = rand(1,10)
D = rand(10,1)
2 commentaires
José-Luis
le 12 Sep 2017
Modifié(e) : José-Luis
le 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.
Jan
le 14 Sep 2017
Modifié(e) : Jan
le 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);
end
Alternatively:
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.
0 commentaires
John BG
le 12 Sep 2017
Modifié(e) : John BG
le 12 Sep 2017
Hi Hany Salem
1.
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
2.
So, Hany Salem
you can simply
rand
ans =
0.3477
or
format long
rand
ans =
0.149997253831683
this is ok as long as the lengths of the generated numbers can be contained within the default number classes.
3.
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
str2num('34567458')
ans =
34567458
as soon as
A =
'2304985273049582364095827340592873425093485273049687'
str2num('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 =
34567458
a start point would be
N=15
A='0.'
for k=1:1:N
A=[A num2str(randi([0 9],1,1))];
end
L=str2num(A)
now despite in the workspace L shows 0.5875
When checking
L =
0.587526066789759
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
0 commentaires
Voir également
Catégories
En savoir plus sur Data Preprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!