This is the mail archive of the ecos-patches@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Libc random generator bug?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]