generate array of Different random floating numbers in a specific range
12 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
Image Analyst
2012 年 2 月 4 日
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.
0 件のコメント
その他の回答 (5 件)
Image Analyst
2012 年 2 月 4 日
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 件のコメント
Md. Al-Imran Abir
2023 年 8 月 27 日
編集済み: Md. Al-Imran Abir
2023 年 8 月 27 日
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.
Image Analyst
2023 年 8 月 27 日
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)
% Round values to nearest .1.
r = round(r/10, 1)
Image Analyst
2012 年 2 月 4 日
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)
0 件のコメント
Bruno Luong
2023 年 8 月 27 日
編集済み: Bruno Luong
2023 年 8 月 27 日
" (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)
upi = floor(up/resolution)
n = 50; % length of your random vector
x = ((loi-1) + randperm((upi-loi)+1, n)) * resolution
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!