How to set upper and lower limit of random number
Show older comments
Hello everyone, I hope you are doing well. i have the following code. it generate a random number
I want to set the upper limit and lower limit for the values and generate a pattern.
for example i have value 350, then the lower limit will be 300 and upper limit will be 350, or any variable which define the upper and lower limit of the dataset., I have tried the following method but it does not work
levels=round(rand*498)+2;
Dataset=round(rand(1,1000)*(levels*1.1))+round(levels*0.5);
scatter(1:length(Dataset),Dataset)
Answers (2)
Voss
on 16 Mar 2022
lower_limit = 300; % to use your example values
upper_limit = 350;
% generate a uniformly distributed random *number* between lower_limit and
% upper_limit:
X = rand()*(upper_limit-lower_limit)+lower_limit
% generate a uniformly distributed pseudorandom *integer* between lower_limit
% and upper_limit:
X = randi(upper_limit-lower_limit+1)+lower_limit-1
10 Comments
Stephen john
on 16 Mar 2022
Voss
on 16 Mar 2022
OK. But what determines the upper and lower limits of the initial random number that you'll add 50 to and subtract 50 from to get the limits of the subsequent random numbers you generate?
random_value = rand(); % random number between 0 and 1
lower_limit = random_value-50;
upper_limit = random_value+50;
% generate a uniformly distributed random *number* between lower_limit and
% upper_limit:
X = rand()*(upper_limit-lower_limit)+lower_limit
% generate a uniformly distributed pseudorandom *integer* between lower_limit
% and upper_limit:
X = randi(upper_limit-lower_limit+1)+lower_limit-1
Stephen john
on 16 Mar 2022
Voss
on 16 Mar 2022
You said they're random, but you didn't say they can't be negative.
random_data = rand(1,1000)*998+2; % 1000 random numbers between 2 and 1000
lower_limit = min(random_data);
upper_limit = max(random_data);
How about that?
Stephen john
on 16 Mar 2022
Voss
on 16 Mar 2022
"I want to generate a random number array of 1000 samples in which the values is between 2 to 1000"
This is excactly what my latest code snippet does in its first line.
"if the number is 300 is selected then the values will be between minum 250 and maximum 350."
If which number is 300? The first one of the 1000 just generated? If any of those 1000 numbers is 300?
And which values will be between 250 and 350? Are you talking about another array of 1000 numbers or what?
Stephen john
on 17 Mar 2022
The upper and lower limit of what?
Is this array of values between 2 and 1000 being used to control the generation of another array?
control = randi([2 1000], 1, 1000);
filtered = nan(size(control));
idx = find(control == 300);
filtered(idx(1)) = randi([250 300], 1, 1); %250 to 300 the first time
idx = idx(2:end); %remaining locations
sometimes = rand(size(idx)) <= .3; %sometimes means a 30% chance, right?
filtered(idx(sometimes)) = randi([250 350], 1, nnz(sometimes));
idx = find(control == 500);
sometimes = rand(size(idx)) <= 0.64; %sometimes means a 64% chance, right?
filtered(idx(sometimes)) = randi([450 550], 1, nnz(sometimes));
Stephen john
on 17 Mar 2022
control = randi([2 1000], 1, 1000);
filtered = nan(size(control));
idx = find(control == 300);
if ~isempty(idx)
%talking about what happens when 300 is first found, is only meaningful
%if 300 was found at all
filtered(idx(1)) = randi([250 300], 1, 1); %250 to 300 the first time
idx = idx(2:end); %remaining locations
end
sometimes = rand(size(idx)) <= .3; %sometimes means a 30% chance, right?
filtered(idx(sometimes)) = randi([250 350], 1, nnz(sometimes));
idx = find(control == 500);
sometimes = rand(size(idx)) <= 0.64; %sometimes means a 64% chance, right?
filtered(idx(sometimes)) = randi([450 550], 1, nnz(sometimes));
%now verify
idx = find(~isnan(filtered))
control(idx)
filtered(idx)
David Hill
on 16 Mar 2022
dataSet=50*rand(1,1000)+300;
8 Comments
Stephen john
on 16 Mar 2022
David Hill
on 16 Mar 2022
Hard to understand your description, but I think you indicate you want a fixed span.
span=100;%span from lowerlimit to upper limit
r=1000*rand-50;%generate randomn number from 0 to 1000 and subtract 50 to get lowerband
dataSet=span*rand(1,1000)+r;
Stephen john
on 16 Mar 2022
David Hill
on 16 Mar 2022
You are not clear with your description. The solution would be very easy if you explained yourself.
- Generate a random number between 2 and 1000 to act a the lower limt
- Generate 1000 samples of data randomly between the lower limit and What?
- Include 50 samples at the lower limit?
None of this is clear to me.
Stephen john
on 16 Mar 2022
David Hill
on 16 Mar 2022
r=998*rand(1,1000)+2;%step one
I have no idea on your other steps. If 300 is selected? How are you selecting it? Do you want other randomn numbers generated it the 100 span range? How many numbers will be selected? How many numbers within the range to you want generated. How do you want them stored?
Stephen john
on 17 Mar 2022
David Hill
on 17 Mar 2022
r=998*rand(1,1000)+2;
span=100;
for k=1:1000
R(k,:)=span*rand(1,50)+r(k)-50;%each row is 50 samples within the +-50 of each element in r
end
Categories
Find more on Random Number Generation 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!