Borrar filtros
Borrar filtros

How to generate a random decimal number between -1 and 1?

3 visualizaciones (últimos 30 días)
Wen Shu Wong
Wen Shu Wong el 29 de Oct. de 2020
Editada: Stephen23 el 29 de Oct. de 2020
I'm trying to use rand below but it is giving me [ ].
rand([-1 1]);

Respuesta aceptada

Stephen23
Stephen23 el 29 de Oct. de 2020
Editada: Stephen23 el 29 de Oct. de 2020
The inputs to rand define its output size, not the output value range. Its range is fixed to 0..1, but you can adjust generate other values using some simple operations:
2*rand()-1
ans = -0.7192

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Oct. de 2020
rand*2-1
Note this will not generate exactly -1 or exactly 1

Categorías

Más información sobre Random Number Generation 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