I need to generate a number randomly which is either -1 or 1

12 次查看(过去 30 天)
Trying to generate either -1 or 1 randomly.
I have looked at;
R = unifrnd(-1,1)
however this generates a random number in the interval (-1,1) whereas I am trying to generate a random number which is either -1 or 1

采纳的回答

Image Analyst
Image Analyst 2016-10-22
Try this:
r = 2 * randi(2) - 3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by