skewness in random numbers

13 次查看(过去 30 天)
tusu
tusu 2015-5-6
回答: Samayochita 2025-2-24,11:00
I am generating 30 random numbers ranging within 8 32 by
w1=randi([8 32],1,30);
Now I need a skewness in this randomly generated..i.e. the avg difference between the elements needs to be smaller,
like 8 8 9 10 10 11 12 13 13 14......like this way
If I could draw this distribution from a normal distribution then the std deviation that's I need to make smaller...
is there any way to do so?

回答(1 个)

Samayochita
Samayochita 2025-2-24,11:00
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:
  1. Generate numbers with a small standard deviation to keep them close to the mean.
  2. Adjust the mean and standard deviation to fit within the desired range [8, 32].
  3. 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.

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by