How to set upper and lower limit of random number

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)

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

@_ But i want upper and lower limit to be random, you just put a single value , i want it to be random. Like if i get any random value then the the pattern will be in 50 minimum and 50 maximum of that value
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
@_ but in your code lower limit is in minus values.
i have random value array of 1000 samples in which random value start from 2 to 1000, and according to that value which randomnly pick we set upper and lower limit
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?
@_ you are not getting my point the code you share above it just generate value between 2 to 1000,
let me explain step by step
  • I want to generate a random number array of 1000 samples in which the values is between 2 to 1000
  • if the number is 300 is selected then the values will be between minum 250 and maximum 350.
  • Then the pattern will be some time between if 300 selected the value is 250 and 350 and sometime if 500 selected it is between 550 and 450 So on.
"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?
@_ in array the values are generated between 1000, in that array the value is 300 then the upper and lower limit will be defined.
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
Index exceeds the number of array elements. Index must not exceed 0.
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));
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))
idx = 1×3
301 707 877
control(idx)
ans = 1×3
500 300 500
filtered(idx)
ans = 1×3
545 294 507

Sign in to comment.

dataSet=50*rand(1,1000)+300;

8 Comments

@David Hill But i want upper and lower limit to be random, you just put a single value , i want it to be random. Like if i get any random value then the the pattern will be in 50 minimum and 50 maximum of that value
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;
@David Hill let me explain you, I random array of 1000 samples, i want to set that random array to minmum limit to 50 samples
You are not clear with your description. The solution would be very easy if you explained yourself.
  1. Generate a random number between 2 and 1000 to act a the lower limt
  2. Generate 1000 samples of data randomly between the lower limit and What?
  3. Include 50 samples at the lower limit?
None of this is clear to me.
@David Hill let me explain step by step
  • I want to generate a random number array of 1000 samples in which the values is between 2 to 1000
  • if the number is 300 is selected then the values will be between minum 250 and maximum 350.
  • Then the pattern will be some time between if 300 selected the value is 250 and 350 and sometime if 500 selected it is between 550 and 450 So on.
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?
@David Hill the numbers are generated between 1-1000, then the upper and lower limit of the numbers depend on the span.
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

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 16 Mar 2022

Commented:

on 17 Mar 2022

Community Treasure Hunt

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

Start Hunting!