aboutsummaryrefslogtreecommitdiff
path: root/nuttx/libc
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-12-02 17:34:08 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-12-02 17:34:08 +0000
commitd128c03666021694c4d2d969061599014f6515c7 (patch)
treeb633352664e92bd1dca221415ebc7fd984b51181 /nuttx/libc
parent3f5c10515649b6925d063ae6e4a1eb3ba0abd8e5 (diff)
downloadpx4-firmware-d128c03666021694c4d2d969061599014f6515c7.tar.gz
px4-firmware-d128c03666021694c4d2d969061599014f6515c7.tar.bz2
px4-firmware-d128c03666021694c4d2d969061599014f6515c7.zip
Fix the fat, flat line bug
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5407 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/libc')
-rw-r--r--nuttx/libc/stdlib/lib_rand.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/nuttx/libc/stdlib/lib_rand.c b/nuttx/libc/stdlib/lib_rand.c
index bbefaee5d..0faef5d66 100644
--- a/nuttx/libc/stdlib/lib_rand.c
+++ b/nuttx/libc/stdlib/lib_rand.c
@@ -174,17 +174,22 @@ static inline void fgenerate2(void)
{
unsigned long randint;
- /* Second order congruential generator. One may be added to the result of the
- * generated value to avoid the value zero (I am not sure if this is necessary
- * for higher order random number generators or how this may effect the quality
- * of the generated numbers).
- */
+ /* Second order congruential generator. */
randint = (RND2_CONSTK1 * g_randint1 +
RND2_CONSTK2 * g_randint2) % RND2_CONSTP;
g_randint2 = g_randint1;
- g_randint1 = (randint == 0 ? 1 : randint);
+ g_randint1 = randint;
+
+ /* We cannot permit both values to become zero. That would be fatal for the
+ * second order random number generator.
+ */
+
+ if (g_randint2 == 0 && g_randint1 == 0)
+ {
+ g_randint2 = 1;
+ }
}
#if (CONFIG_LIB_RAND_ORDER == 2)
@@ -207,11 +212,7 @@ static inline void fgenerate3(void)
{
unsigned long randint;
- /* Third order congruential generator. One may be added to the result of the
- * generated value to avoid the value zero (I am not sure if this is necessary
- * for higher order random number generators or how this may effect the quality
- * of the generated numbers).
- */
+ /* Third order congruential generator. */
randint = (RND3_CONSTK1 * g_randint1 +
RND3_CONSTK2 * g_randint2 +
@@ -219,7 +220,16 @@ static inline void fgenerate3(void)
g_randint3 = g_randint2;
g_randint2 = g_randint1;
- g_randint1 = (randint == 0 ? 1 : randint);
+ g_randint1 = randint;
+
+ /* We cannot permit all three values to become zero. That would be fatal for the
+ * third order random number generator.
+ */
+
+ if (g_randint3 == 0 && g_randint2 == 0 && g_randint1 == 0)
+ {
+ g_randint3 = 1;
+ }
}
static double_t frand3(void)