问题
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