help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Rand implementation question


From: mpender
Subject: Rand implementation question
Date: Thu, 27 Feb 2014 18:45:18 -0800 (PST)

I have had intermittent strange behavior when attempting to use the rand
function to create binary digits that I would like advice about.

I usually use an expression like the following to produce a random binary
digit of either '0' or '1'
digits = round (rand(size(1:8)) + 0.5)

This normally returns either a 0 or a 1. But sometimes I get some difficult
to trace array index out of bound errors when I use this as an array index. 
I suspect that the rand function may be returning a value between 0 and 1
(inclusive), and on the rare occasion that rand throws the '1' value, then:
round (1 + 0.5) = 2 , which results in my index out of bounds condition.

I am familiar with a limitation of other implementations of the Mersenne
Twister that return a value between 0 (exclusive) and 1 (inclusive) -- does
anyone know if the Octave implementation works this way?  If so, then I
could replace rand with  (1 - rand) to get a value between 0 (inclusive) and
1 (exclusive) which would work fine for me.  Of course, it won't do any good
if the implementation can return both 0 (inclusive) and 1 (inclusive).

Otherwise, can anyone recommend an efficient way to extract binary digits
from the rand function?  According to the profiler, generating random values
is already one of the most time-consuming processes in my scripts, so I
don't want to make the process even slower.

Thanks, Mike



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Rand-implementation-question-tp4662471.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]