Generating random integer from -x to x

I need to find a way to generate a random integer from -x to x for example -5 to 5

 Akzeptierte Antwort

Chris C
Chris C am 12 Mär. 2014
Bearbeitet: Chris C am 12 Mär. 2014

0 Stimmen

xmin = -x;
xmax = x;
r = round((xmax-xmin).*rand(10,1)+xmin);

Weitere Antworten (1)

per isakson
per isakson am 12 Mär. 2014
Bearbeitet: per isakson am 12 Mär. 2014

1 Stimme

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);

Gefragt:

am 12 Mär. 2014

Bearbeitet:

am 12 Mär. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by