-4

I want to generate random number that follows a normal distribution and within some range say [3,17]. I tried normrnd but i can't specify the range for it.

Fadwa
  • 1,717
  • 5
  • 26
  • 43
  • 2
    "Normal distribution" and "within some range" are incompatible requirements. A normal random variable has an infinite range – Luis Mendo May 27 '14 at 11:25
  • 'Normal distribution' is centered at `0` (can be shifted by addition) and is defined with 'mu,sigma'. If you want a random distribution ranging from 3to17 (all values more or less equal distributed) try: `round(rand(1,1000)*14+3)` this results in integer values from 3 to 17(14+3). If you don't want just the integer values delete/modify the `round`-command – The Minion May 27 '14 at 11:35
  • @TheMinion But `rand` generates numbers with uniform distribution not normal distribution. – Fadwa May 27 '14 at 11:39
  • Can i use `abs(randn(1)*14+3)` ? – Fadwa May 27 '14 at 11:41
  • @Misaki. Abs shouldn't have any effect since the absolut of a positive value is the value itself. And yes my code results in a uniform distribution. I don't really understand what your distribution should look like. As Luis Mendo wrote in his comment. There is no Normal distribution with certain range. Do you want a gaussian centered at 10 with a width of 7? could you draw/post a picture of the graph? – The Minion May 27 '14 at 12:31
  • Wiki page for the [Normal (Gaussian) Distribution](https://en.wikipedia.org/wiki/Normal_distribution). The discussion over the distribution's support implies a quick look at the distribution's mathematics would be insightful. – SecretAgentMan Jun 04 '19 at 18:28

1 Answers1

2

The stats toolbox allows creation of truncated distributions

Warning: This is no longer a normal distribution

The data will no longer follow a normal distribution, assumptions will be invalidated, and is generally not a good idea...

With no mean or standard deviation given for the distribution the following values will be used:

mu = 10; %center of the range
sigma = 14/6; % range covers mu + or - 3*s.d.

The distribution can be made and truncated as follows

pd=makedist('normal','mu',mu,'sigma',sigma)
pd=truncate(pd,3,17);

A sample of given size can be generated from this distribution with random

nrows=10;
ncols=5;  
data=random(pd,ncols,nrows);

Example

hist(random(pd,10e6,1),10000)

enter image description here

It can be seen the data fall between 3 and 17, and appears similar to a normal distribution within these bounds... but due to the truncation it is no longer normal

Community
  • 1
  • 1
RTL
  • 3,577
  • 15
  • 24