[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Random number generation quirk
From: |
mpender |
Subject: |
Random number generation quirk |
Date: |
Thu, 27 Feb 2014 18:56:59 -0800 (PST) |
I have had intermittent strange behavior when attempting to use the rand
function to create random numbers that I would like advice about.
I usually use an expression like the following to produce random numbers
between 1 and 10
digits = round (rand*10 + 0.5)
This normally returns a number between 1 and 10. But sometimes I get some
difficult to trace array index out of bound errors when I use the computed
value as an array index. I suspect that sometimes the rand function returns
exactly 1.0 and then round (1.0 * 10 + 0.5) = 11 , 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 a better way to generate
random numbers? This is the way I have always seen it done before.
Thanks, Mike
--
View this message in context:
http://octave.1599824.n4.nabble.com/Random-number-generation-quirk-tp4662473.html
Sent from the Octave - General mailing list archive at Nabble.com.
- Random number generation quirk,
mpender <=