How To Generate Non Repeating floating Random Numbers from 1 to 10

Hi,
Does anybody know how to generate a series of 100 non-repeating random floating numbers between 1 and 10?
Thanks

 Respuesta aceptada

One option:
v = rand(1, 100)*9+1;
[L, H] = bounds(v) % Check
producing (in this run):
L =
1.0846
H =
9.9740

6 comentarios

This code does not strictly guarantee that the values will be non-repeating. In practice, though, I have never seen duplicate rand() values show up except when I manipulated the random number seed.
I also have never seen them repeat. I tested that at one point by using unique:
Check = numel(unique(v))
This has always been the same as numel(v) when I have tried it.
I just did a test with 5e8 and got back 5e8 minus 9 unique, so it can happen with large enough arrays.
With a pseudo-random random number generator, I’m certain that’s possible. With a vector of only 100 numbers, several trials (in a loop) have always found equality between the element numbers of the vector and its unique elements, although with enough trials, there are bound to occasionally be repeats. Noting that is a probability of , so it is not likely to be a significant issue with a vector of 100 elements.
With 2^53 possible rand() outputs, an estimation using The Birthday Paradox logic suggests that the probability of a duplicate rises to 1/2 at about 10^8
Noted.
I’d not thought to test this with the ‘Birthday Party’ problem.

Iniciar sesión para comentar.

Más respuestas (1)

z = cumsum(rand(100,1));
mn = min(z);
Z = 9/(max(z) - mn)*(z - mn) + 1;
out = Z(randperm(100));

Categorías

Más información sobre Birthdays en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Nov. de 2019

Comentada:

el 26 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by