Hi All,
I have some issues with the rand function in msp430-libc,
include/stdio.h says,
#ifndef RAND_MAX
#define RAND_MAX 0x7fffffffL
#endif
include/limits.h says,
#define INT_MAX 32767 /* maximum (signed) int value */
...
#ifndef RAND_MAX
#define RAND_MAX INT_MAX
#endif
So you get a different RAND_MAX depending on the order which you include
files?
The linux man page for int rand() says that rand() generates a random
number from 0 to RAND_MAX, the msp430-libc implementation generates a
int rand from -32768 to 32767.
I don't want to get into arguments about the randomness of rand(), it
does what I want just fine, although there are a few problems with the
implementation, that I think need fixing.
Attached is a diff file against the msp430-libc to fix both of these
problems. Could someone put this into the CVS.
--
Peter Jansen
___________________________________________________________________________
Australian Antarctic Division - Commonwealth of Australia
IMPORTANT: This transmission is intended for the addressee only. If you are not
the
intended recipient, you are notified that use or dissemination of this
communication is
strictly prohibited by Commonwealth law. If you have received this transmission
in error,
please notify the sender immediately by e-mail or by telephoning +61 3 6232
3209 and
DELETE the message.
Visit our web site at http://www.aad.gov.au/
___________________________________________________________________________
Index: include/stdlib.h
===================================================================
RCS file: /cvsroot/mspgcc/msp430-libc/include/stdlib.h,v
retrieving revision 1.11
diff -r1.11 stdlib.h
107c107
< #define RAND_MAX 0x7fffffffL
---
> #define RAND_MAX 0x7fffL
Index: src/stdlib/rand.c
===================================================================
RCS file: /cvsroot/mspgcc/msp430-libc/src/stdlib/rand.c,v
retrieving revision 1.2
diff -r1.2 rand.c
52c52,63
< return ((*ctx = *ctx * 1103515245 + 12345) % ((unsigned long int)RAND_MAX
+ 1));
---
> long hi, lo, x;
>
> hi = *ctx / 127773L;
> lo = *ctx % 127773L;
> x = 16807L * lo - 2836L * hi;
> if (x <= 0)
> x += 0x7fffffffL;
>
> *ctx = (unsigned long)x;
> x = x % ((unsigned long)RAND_MAX_X +1);
>
> return x;