This is the mail archive of the
ecos-patches@sourceware.org
mailing list for the eCos project.
Libc random generator bug?
- From: Fredrik Hederstierna <fredrik at hederstierna dot com>
- To: ecos-patches at ecos dot sourceware dot org
- Date: Tue, 17 Mar 2009 14:17:43 +0100
- Subject: Libc random generator bug?
- Reply-to: fredrik at hederstierna dot com
Hi
I suspect that rand() is not working in eCos libc if using Knuth algorithm:
File: rand.cxx, line 197
*seed = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
if (*seed < 0)
*seed += MM;
Since "seed" variable is unsigned pointer, the comparison with 0 will always result FALSE.
I dont know if this is the idea, but maybe something like this could help to use a temporary signed varialbe
- *seed = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
- if (*seed < 0)
- *seed += MM;
+ signed int s = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
+ *seed = (unsigned int)s;
+ if (s < 0)
+ *seed += MM;
Best Regards
/Fredrik