Electronics & Programming


Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » MSPGCC
  • » [Mspgcc-users] msp430-libc rand function [RSS Feed]

#1 Nov. 9, 2005 00:26:31

Peter J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[Mspgcc-users] msp430-libc rand function

Hi All,

I have some issues with the rand function in msp430-libc,

include/stdio.h says,

#ifndef RAND_MAX
#define RAND_MAX 0x7fffffffL

include/limits.h says,

#define INT_MAX 32767 /* maximum (signed) int value */
#ifndef RAND_MAX
#endifSo you get a different RAND_MAX depending on the order which you includefiles?The linux man page for int rand() says that rand() generates a randomnumber from 0 to RAND_MAX, the msp430-libc implementation generates aint rand from -32768 to 32767.I don't want to get into arguments about the randomness of rand(), itdoes what I want just fine, although there are a few problems with theimplementation, that I think need fixing.Attached is a diff file against the msp430-libc to fix both of theseproblems. 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
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 athttp://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
< #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
< 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;


  • Root
  • » MSPGCC
  • » [Mspgcc-users] msp430-libc rand function [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of April

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support