# Generation of random numbers

9 visualizaciones (últimos 30 días)
SURBHIT el 27 de Mayo de 2014
Editada: Mahdi el 27 de Mayo de 2014
How can I generate uniformly distributed random numbers in the range 10^(-4) to 10^(+4)? I tried using rand function but it does not give me the required range. This is what I tried:
dd = (10^(4) - 10^(-4)).*rand(1000,1) + 10^(-4); plot(dd);
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Mahdi el 27 de Mayo de 2014
Editada: Mahdi el 27 de Mayo de 2014
I think what you're trying to do is done using randn, so that
dd=(10^(4) - 10^(-4)).*randn(1000,1) + 10^(-4)
hist(dd,1000) # To see it in a histogram with 1000 bins
Note the range of x-axis
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Mahdi el 27 de Mayo de 2014
Then what you did is correct. It looks quite normal to me.
John D'Errico el 27 de Mayo de 2014
NO. randn is NOT the solution here, since it generates numbers that can fall ANYWHERE in theory.

Iniciar sesión para comentar.

### Más respuestas (3)

Shashank Prasanna el 27 de Mayo de 2014
That sounds about right. Take a look at the first example in the documentation:
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
SURBHIT el 27 de Mayo de 2014
but the output is only in the range 10^(-1) to 10^(+4) how can i get values below 10^(-1)? i am not able to generate the smaller values

Iniciar sesión para comentar.

Roger Wohlwend el 27 de Mayo de 2014
You use the correct method. When your range is from 10^(-4) to 10^4 it is quite normal that values below 10^(-1) are very, rare.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

John D'Errico el 27 de Mayo de 2014
I think the problem is that while you SAY you want numbers that are uniformly distributed over that interval, you don't really WANT them uniformly distributed.
The probability is actually quite small that for an interval like that, that you will see numbers less than say 1. In fact, suppose we did generate a uniform randm number in the interval [1e-4,1e4]?
What is the probability that any given such deviate will be in the sub-interval [1e-4,1]? This is easy to compute.
(1 - 1e-4)/(1e4 - 1e-4)
ans =
9.999e-05
Essentially, you need to have many such samples before you ever see a SINGLE such deviate in the sub-interval.
So while you SAY you want uniform, I think you really want numbers that are uniformly distributed in a log domain. That is, the logs of your deviates will be uniformly distributed.
% these numbers will be uniform over the interval [-4,4]
E = 8*rand(1,10000) - 4;
% The numbers in R will have the distribution you desire
R = 10.^E;
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Uniform Distribution (Continuous) en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by