Octave - random generate number

十年热恋 提交于 2020-01-03 17:21:09

问题


I have a simple question about randomly generating numbers in Octave/Matlab.

How do I randomly generate a (one!) number (that is either 0 or 1)?

I could really use an example.

Thanx


回答1:


Use rand, which generates a uniform pseudo-random number in the range 0..1, and then test this value against a suitable threshold, e.g. 0.5 for equal probability of 1 or 0:

r = rand > 0.5



回答2:


You should use randi for integer random numbers:

randi(2) - 1



回答3:


For the sake of variety, here's another way

floor(rand*2)


来源:https://stackoverflow.com/questions/16300803/octave-random-generate-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!