How can I generate a random number that is negative or positive and between two other values?
184 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lena Berard
el 20 de Nov. de 2015
Comentada: Jacob
el 11 de Oct. de 2023
I need to write a function that generates two numbers that are between the negative and positive values of an integer. For example, if I call the function and input 7 it should generate two random numbers between -7 and +7. I can't figure out a way to use the randi function because the values I need can't be just integers.
0 comentarios
Respuesta aceptada
Walter Roberson
el 20 de Nov. de 2015
Hint: rand()-1/2 is in the range -1/2 to +1/2
1 comentario
Jacob
el 11 de Oct. de 2023
if you a number x
generating random between -x and x
x = 7;
for i = 1:x*2
z(i) = (2*x)*(rand(1)-.5);
end
disp(z)
Más respuestas (2)
Rahul Gulia
el 22 de En. de 2020
>> randi([-2,2],3,3) will give the desired output.
Thanks for pointing out my mistake @ Walter Roberson .
0 comentarios
Ver también
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!