Hi Tusu,
I understand that you want to generate random numbers between 8 and 32, but instead of the numbers being completely random, they should be closer together (less variation). You can control the spread of randomly generated numbers by drawing from a normal distribution using ‘randn’
You can follow the steps below to achieve this:
- Generate numbers with a small standard deviation to keep them close to the mean.
- Adjust the mean and standard deviation to fit within the desired range [8, 32].
- Ensure the values stay in the range [8, 32] by rounding and clamping.
I have provided the sample code below, additionally you can sort the values before displaying them using ‘sort’
(https://www.mathworks.com/help/matlab/ref/double.sort.html) to ensure they are in ascending order (as shown in your example).
n = 30; % Number of random values
mu = 20; % Mean of the distribution (center of the range)
sigma = 3; % Small standard deviation for less spread
w1 = round(mu + sigma * randn(1, n)); % Generate numbers using normal distribution
% Clamp values within [8, 32]
w1(w1 < 8) = 8;
w1(w1 > 32) = 32;
% Optional -> sort in ascending order
w1 = sort(w1);
% Display the result
disp(w1);
I hope this works for you.