How to generate random numbers between two negative or one negative and one positive number?

조회 수: 35 (최근 30일)
The random generation should fall under normal or poisson distribution.
I have been trying to generate a set of 100 new numbers from previous 10 numbers. The new numbers generated must fall near 2 to 3 deviations from the mean. The problem is that when I have negative numbers, the random function gives me NaN number. It is important for me to keep those negative numbers for the generation since they are a result of a previous calculation in the code. Do we have another way to go about this? All suggestions are highly appreciated. Thanks in advance
  댓글 수: 1
Stephen23
Stephen23 2019년 1월 26일
"The problem is that when I have negative numbers, the random function gives me NaN number"
Which "random function" are you talking about?
In a new comment, please show us the exact code that you tried.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrey Kiselnikov
Andrey Kiselnikov 2019년 1월 26일
편집: Andrey Kiselnikov 2019년 1월 26일
Hi, X = rand returns a single uniformly distributed random number in the interval (0,1). Then you can multiply it and add constants.
For example, X = rand*2 -1; will return random number between (-1,1).

추가 답변 (1개)

Khush
Khush 2019년 1월 26일
편집: Khush 2019년 1월 26일
Thank you so much for the help Mr. Stephan Cobeldick and Mr. Andrey Kiselnikov.
Here is the code. Please have a look
x1coord=[49.1507356667592;82.7970961954887;148.659892354319;-308.818954581290;-1010.68213895169;-46.5674421562209;-151.626765574156;517.972982735331;62.6507242012784;-44.3875709393678];
x1=x1coord(1);
x2=x1coord(2);
x1coord(11)=x2;
x1coord(2)=0;
x3=x1coord(3);
x1coord(21)=x3;
x1coord(3)=0;
x4=x1coord(4);
x1coord(31)=x4;
x1coord(4)=0;
x5=x1coord(5);
x1coord(41)=x5;
x1coord(5)=0;
x6=x1coord(6);
x1coord(51)=x6;
x1coord(6)=0;
x7=x1coord(7);
x1coord(61)=x7;
x1coord(7)=0;
x8=x1coord(8);
x1coord(71)=x8;
x1coord(8)=0;
x9=x1coord(9);
x1coord(81)=x9;
x1coord(9)=0;
x10=x1coord(10);
x1coord(91)=x10;
x1coord(10)=0;
%rng='default';
for i=2:1:10
x1coord(i)= random('poisson',x1);
end
for i=12:1:20
x1coord(i)= random('poisson',x2);
end
for i=22:1:30
x1coord(i)= random('poisson',x3);
end
for i=32:1:40
x1coord(i)= random('poisson',x4);
end
for i=42:1:50
x1coord(i)= random('poisson',x5);
end
for i=52:1:60
x1coord(i)= random('poisson',x6);
end
for i=62:1:70
x1coord(i)= random('poisson',x7);
end
for i=72:1:80
x1coord(i)= random('poisson',x8);
end
for i=82:1:90
x1coord(i)= random('poisson',x9);
end
for i=92:1:100
x1coord(i)= random('poisson',x10);
end
Therefore when x3 is negative, I get NaN number in the matrix. x1coord is just one column out of the 7 different columns that are created in a 100x7 matrix using similar method.
I tried using uniform distribution as well as normal distribution using correct syntax for the parameters but same issue arises due to negative numbers.
Thank you once again for your help and suggestions.

카테고리

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