I'm looking for a function that will generate random values between 0
and 1
, inclusive. I have generated 120,000 random values by using rand()
function in octave, but haven't once got the values 0
or 1
as output. Does rand()
ever produce such values? If not, is there any other function I can use to achieve the desired result?
If you read the documentation of rand
in both Octave and MATLAB, it is an open interval between (0,1)
, so no, it shouldn't generate the numbers 0 or 1.
However, you can perhaps generate a set of random integers, then normalize the values so that they lie between [0,1]
. So perhaps use something like randi
(MATLAB docs, Octave docs) where it generates integer values from 1 up to a given maximum. With this, define this maximum number, then subtract by 1 and divide by this offset maximum to get values between [0,1]
inclusive:
max_num = 10000; %// Define maximum number
N = 1000; %// Define size of vector
out = (randi(max_num, N, 1) - 1) / (max_num - 1); %// Output
If you want this to act more like rand
but including 0 and 1, make the max_num
variable quite large.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With