Borrar filtros
Borrar filtros

Generating random integer from -x to x

2 visualizaciones (últimos 30 días)
Austin
Austin el 12 de Mzo. de 2014
Editada: per isakson el 12 de Mzo. de 2014
I need to find a way to generate a random integer from -x to x for example -5 to 5

Respuesta aceptada

Chris C
Chris C el 12 de Mzo. de 2014
Editada: Chris C el 12 de Mzo. de 2014
xmin = -x;
xmax = x;
r = round((xmax-xmin).*rand(10,1)+xmin);

Más respuestas (1)

per isakson
per isakson el 12 de Mzo. de 2014
Editada: per isakson el 12 de Mzo. de 2014
2*x*rand-x produces uniformly distributed numbers
Doc says: Example 1, Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);

Community Treasure Hunt

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

Start Hunting!

Translated by