Generate Random Number, that is that is repeatable

1 view (last 30 days)
Hi,
I have generated a code in order to replace the entire original column, with random repeated value but it doesnt give the desired output
% Code is
if range(Selected_Data1(:,1)) ==0
for Iter=1:length(Selected_Data1)
rng(1);
P_MMSI(Iter) = rand(1);
end
end
The above code always replace the value 209026000 with 0.417022004702574, nevertheless, I am expecting the random output vector would be 9 interger numbers (e.g., 781234012)
For example, if the original column is
209026000
209026000
209026000
209026000
209026000
209026000
209026000
209026000
209026000
209026000
% The expected output would be something
781234012
781234012
781234012
781234012
781234012
781234012
781234012
781234012
781234012

Accepted Answer

Steven Lord
Steven Lord on 26 Jul 2022
So you want to replace a certain section of your P_MMSI vector with the same random number? Let's look at your original code:
% Code is
if range(Selected_Data1(:,1)) ==0
for Iter=1:length(Selected_Data1)
Don't use length in this context. From the way you're using Selected_Data1 in your if statement it's a matrix. In that case, if you want to replace a number of elements in P_MMSI equal to the number of rows in Selected_Data1, say that.
rng(1);
P_MMSI(Iter) = rand(1);
You don't need to call rng and rand every iteration in your for loop. Generate and store the number once and use that value over and over.
end
end
Here's how I'd probably write this code, assuming that your selected data has no NaN values. If it does you'd want to check that they are all NaN values using isnan. You could put the code that defines the isColumnConstant variable directly in the if statement condition, but I prefer separating them as I find the descriptive variable name makes it easier to read and understand the code. I haven't tested this but I expect it to work.
% If range is 0, all the elements in the vector must equal the first element
%
% Note that this won't work if Selected_Data1 has no rows or no columns
isColumnConstant = all(Selected_Data1(:, 1) == Selected_Data1(1, 1));
if isColumnConstant
% Generate a random 9-digit value
replacementValue = randi([1e8, 1e9-1])
% Fill in the appropriate elements in P_MMSI with the replacement value
P_MMSI(1:height(Selected_Data1)) = replacementValue;
end
  3 Comments
neamah hasan
neamah hasan on 26 Jul 2022
Dear @Steven Lord, thanks again I have fixed the error by using
P_MMSI(1:size(Selected_Data1)) = replacementValue;
rather than
P_MMSI(1:height(Selected_Data1)) = replacementValue;
I have another question, and your help is highly appreciated
For example,
Original Column is
209026000
209026000
209026000
209026000
209026000
209026000
I need the Output is by replacing the last two or three digits of the original input
209026108
209026108
209026108
209026108
209026108
209026108
Steven Lord
Steven Lord on 26 Jul 2022
Don't use the 1-input form of size. Specify explicitly that you want the size in the first dimension.
M = ones(3, 5);
size(M, 1)
ans = 3
When you say "replacing the last two or three digits of the original input" do you mean that you want to constrain the last two or three digits of those 9-digit numbers to be the same as some value? If so make a number that's a multiple of 1000 (so the last three digits are 000) and add your fixed value to it.
fixedLastThreeDigits = 123;
randomNumbers = 1000*randi([1e5, 1e6-1], 5, 1)
randomNumbers = 5×1
339449000 422894000 386491000 687664000 757373000
N = randomNumbers + fixedLastThreeDigits
N = 5×1
339449123 422894123 386491123 687664123 757373123

Sign in to comment.

More Answers (1)

David Hill
David Hill on 26 Jul 2022
Use randi
randi(1e9,1)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!