user1926550
user1926550

Reputation: 695

Octave - random generate number

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

Upvotes: 4

Views: 13472

Answers (3)

Kermit the Hermit
Kermit the Hermit

Reputation: 379

For the sake of variety, here's another way

floor(rand*2)

Upvotes: 1

juliohm
juliohm

Reputation: 3779

You should use randi for integer random numbers:

randi(2) - 1

Upvotes: 6

Paul R
Paul R

Reputation: 212929

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

Upvotes: 6

Related Questions