Shop OBEX P1 Docs P2 Docs Learn Events
C language - rand() datatype (max value) — Parallax Forums

C language - rand() datatype (max value)

John KauffmanJohn Kauffman Posts: 653
edited 2015-03-24 22:54 in Propeller 1
In one source I see Propeller C rand() generates from 0 to 2,147,483,647, implying data type of int.
But I generated 100k rand() values and got a range from 0 to 32767 implying a data type of short int, just the positives.
A general C reference said it depends on the implementation.

Can anyone confirm the data type, or why my test for max is 32767 instead of 2,147,483,647?

test code:
#include "simpletools.h"
int main(void)
{
srand(CNT);
int myRandNew=1; int countTrial=1;
int maxSoFar=1; int minSoFar=1;

for(countTrial;countTrial<=100000;countTrial++){
myRandNew=rand();
if(myRandNew>maxSoFar) maxSoFar=myRandNew;
if(myRandNew<minSoFar) minSoFar=myRandNew;
}//for
print("count of trials %d\nminSoFar=%d\tmaxSoFar=%d\n",countTrial,minSoFar,maxSoFar);
}

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2015-03-24 12:31
    The source for the rand function is
    #define A 1103515245
    #define C 12345
    #define S 16  /* amount to shift before retrieving result */
    
    int
    rand(void)
    {
      _TLS->rand_seed = _TLS->rand_seed * A + C;
      return (_TLS->rand_seed >> S) & RAND_MAX;
    }
    
    RAND_MAX is defined to be 0x7fff. So rand returns an int that is between 0 and 32767.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-24 12:37
    Interesting, according to my rand man page:

    "The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive"

    However it does not say what RAND_MAX might be.

    It goes on to:

    "POSIX.1-2001 gives the following example of an implementation of rand() and srand(), possibly useful when one needs the same sequence on two different machines.
               static unsigned long next = 1;
    
               /* RAND_MAX assumed to be 32767 */
               int myrand(void) {
                   next = next * 1103515245 + 12345;
                   return((unsigned)(next/65536) % 32768);
               }
    
               void mysrand(unsigned int seed) {
                   next = seed;
               }
    
    All in all I would never use rand(). Firstly because it is generally a really bad pseudo random number generator and secondly because it is not thread safe (for that you should use rand_r()).

    Like many C standard functions it is to be avoided.

    Can you printf the value of RAND_MAX ? That will tell you something.
  • yetiyeti Posts: 818
    edited 2015-03-24 12:38
    (yeti@aurora:8)~/wrk/propeller/propgcc/propgcc/lib/include$ grep -i rand stdlib.h 
    #define RAND_MAX    0x7fff
      int rand(void);
      void srand(unsigned int seed);
    

    ... and reading lib/stdlib/rand.c ...
    int
    rand(void)
    {
      _TLS->rand_seed = _TLS->rand_seed * A + C;
      return (_TLS->rand_seed >> S) & RAND_MAX;
    }
    

    ...looks like propgcc4's lib produces 15-bit random integers...
    (propgcc4-1.9.whatsowver-version here...)
  • John KauffmanJohn Kauffman Posts: 653
    edited 2015-03-24 13:17
    Thanks for discussion. Looks like combined methods of analyses point to 32767.
    Heater. wrote: »
    Can you printf the value of RAND_MAX ? That will tell you something.

    I tried below but rand_max not recognized (tried UC and LC). Do I have to do something more to "pull it out from the library"?
    #include "simpletools.h"
    int main(void){
    srand(CNT); 
    int myRandNew=1;
    myRandNew=rand();
    //print("\nrand_max = %d",RAND_MAX());
    //print("\nrand_max = %d",rand_max());
    print("\nrand_max = %d",rand_Max());
    }
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-03-24 13:22
    You have to include stdlib.h.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2015-03-24 14:54
    This may be of interest to random persons http://web.stanford.edu/class/ee380/Abstracts/150218.html The linked PCG website has C code.

    John Abshier
  • Heater.Heater. Posts: 21,230
    edited 2015-03-24 22:54
    John,

    Wow thanks for the heads up on PGC. Melissa O'Neill's lecture on random number generators is a real treat for us random nerds.

    I had not realized there were such recent developments in PRNGs and statistical tests for randomness. (TestU01)

    Somewhere here I posted a Spin object for the jkiss32 PRNG, a version of George Marsaglia's KISS algorithm, which dates back to 1999 http://www.math.niu.edu/~rusin/known-math/99/RNG, looks like we now need a Spin / PASM PGC!
Sign In or Register to comment.