generate array of Different random floating numbers in a specific range

Dear all, I am going to generate an array of Different random floating numbers in a specific range( such as range of 1, 100) in MATLAB. randperm is creating integer, but I want float numbers , preferable with 1 decimal point. Like 20.1 , 34.2 , 20.2, 27.4 Any help is really appreciated . Thanks

 Respuesta aceptada

If you want 150 numbers in the range of 1-100, try this (small modification of my prior code):
numberOfElementsNeeded = 150;
while true % Loop until we get the required number of elements.
% Create an array of what should be more than enough numbers with 1 decimal
% place.
% We use more than enough in case there are duplicates that we need to remove.
% It was specified that they all need to be different so there's a
% possibility that some may need to be removed (if there are duplicates).
m = double(int32(rand(3*numberOfElementsNeeded,1) * 1000)) / 10;
% Find out if any happen to be identical.
mUnique = unique(m);
% unique() sorts them. Randomize them to undo the sort
finalArray = m(randperm(length(mUnique)));
% Grab only the number of elements that we need.
numElementsToTake = min([numberOfElementsNeeded length(finalArray)]);
% See if we have enough.
if numElementsToTake == numberOfElementsNeeded
% If we have enough, then break out of the loop.
break;
end
% If we didn't get enough, try again.
end
% Take just numberOfElementsNeeded of them
finalArray = finalArray(1:numberOfElementsNeeded);
% Print out to command window
finalArray
I just changed the line before the for loop to
numberOfElementsNeeded = 150;
and added the line
finalArray = finalArray(1:numberOfElementsNeeded);
after the loop.

Más respuestas (5)

I think this code I wrote is fairly robust and flexible:
numberOfElementsNeeded = 100;
while true % Loop until we get the required number of elements.
% Create an array of what should be more than enough numbers with 1 decimal
% place.
% We use more than enough in case there are duplicates that we need to remove.
% It was specified that they all need to be different so there's a
% possibility that some may need to be removed (if there are duplicates).
m = double(int32(rand(3*numberOfElementsNeeded,1) * 1000)) / 10;
% Find out if any happen to be identical.
mUnique = unique(m);
% unique() sorts them. Randomize them to undo the sort
finalArray = m(randperm(length(mUnique)));
% Grab only the number of elements that we need.
numElementsToTake = min([numberOfElementsNeeded length(finalArray)]);
% See if we have enough.
if numElementsToTake == numberOfElementsNeeded
% If we have enough, then break out of the loop.
break;
end
% If we didn't get enough, try again.
end
% Print out to command window
finalArray

6 comentarios

P.S. I hope this wasn't your homework! If so, ignore it.
No this is not any homw work, Actually I am working on research. Thanks for consideration.
@Image Analyst Hello! Has there been any new function added for this in later versions? I am asking as I couldn't found any similar thread from recent times.
How about
format long g
% Define parameters.
minValue = 1;
maxValue = 1000;
numberToGet = 9;
% Generate a set of floating point numbers.
r = minValue + (maxValue - minValue) * rand(1, numberToGet)
r = 1×9
602.200629477027 254.508319839586 586.850933385989 413.251448689176 336.006749585505 637.57299397731 540.719970607523 896.675054360822 919.776384436408
% Round values to nearest .1.
r = round(r, 1)
r = 1×9
602.2 254.5 586.9 413.3 336 637.6 540.7 896.7 919.8
Md. Al-Imran Abir
Md. Al-Imran Abir el 27 de Ag. de 2023
Editada: Md. Al-Imran Abir el 27 de Ag. de 2023
I think this might no't ensure that there won't be any repetition though I didn't get any repitition while I ran it several times. I am not sure about it as I don't know anything about the underlying algorithm but I think when I will run it for thousands of times in loops, there might be some repitition.
There may be repeats after the array is rounded to the nearest 10th. To avoid that use randperm with 10 times the max
format long g
% Define parameters.
minValue = 1;
maxValue = 1000;
numberToGet = 9;
% Generate a set of floating point numbers.
r = minValue + randperm((maxValue - minValue), numberToGet)
r = 1×9
299 955 426 922 86 582 207 757 191
% Round values to nearest .1.
r = round(r/10, 1)
r = 1×9
29.9 95.5 42.6 92.2 8.6 58.2 20.7 75.7 19.1

Iniciar sesión para comentar.

Another way that is not quite as random and not quite as robust or flexible as my first answer:
m = double(randperm(1000))/10;
finalArray = m(1:100)
saharsahar
saharsahar el 4 de Feb. de 2012
  • Thanks Image Analyst, These two methods works well when the total number of those numbers to be generated will be less than the integers in the range , But if I want to create for example 150 random floating numbers (with 1 decimal point) in a range(1:100) , it will not work, and I think it is beacuse of presence of randperm. Any idea is really appreciated.
saharsahar
saharsahar el 4 de Feb. de 2012
That is exactly what I want. Many Thanks. However, I'm curious to know where the upper bound of the range (in this example 100) is mentioned in the code? I may change the upper bound to other numbers such as 25. I really appreciate it.
" (1, 100) in MATLAB. randperm is creating integer, but I want float numbers , preferable with 1 decimal point. Like 20.1 , 34.2 , 20.2, 27.4 "
lo = 1;
up = 100;
resolution = 0.1;
loi = ceil(lo/resolution)
loi = 10
upi = floor(up/resolution)
upi = 1000
n = 50; % length of your random vector
x = ((loi-1) + randperm((upi-loi)+1, n)) * resolution
x = 1×50
87.2000 25.8000 25.1000 82.5000 95.3000 83.9000 39.7000 84.7000 42.3000 23.0000 80.9000 10.6000 30.8000 67.5000 48.2000 7.2000 88.0000 34.6000 48.3000 92.2000 23.3000 30.9000 51.1000 11.2000 82.1000 80.8000 97.3000 69.9000 94.3000 50.1000

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Feb. de 2012

Comentada:

el 27 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by