Back to index

glibc  2.9
tst-rand48-2.c
Go to the documentation of this file.
00001 #include <stdint.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 
00006 int
00007 main (void)
00008 {
00009   time_t t = time (NULL);
00010   int i, ret = 0;
00011   double d;
00012   long int l;
00013   struct drand48_data data;
00014   unsigned short int buf[3];
00015 
00016   srand48 ((long int) t);
00017   for (i = 0; i < 50; i++)
00018     if ((d = drand48 ()) < 0.0 || d >= 1.0)
00019       {
00020         printf ("drand48 %d %g\n", i, d);
00021         ret = 1;
00022       }
00023 
00024   srand48_r ((long int) t, &data);
00025   for (i = 0; i < 50; i++)
00026     if (drand48_r (&data, &d) != 0 || d < 0.0 || d >= 1.0)
00027       {
00028         printf ("drand48_r %d %g\n", i, d);
00029         ret = 1;
00030       }
00031 
00032   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00033   for (i = 0; i < 50; i++)
00034     if ((d = erand48 (buf)) < 0.0 || d >= 1.0)
00035       {
00036         printf ("erand48 %d %g\n", i, d);
00037         ret = 1;
00038       }
00039 
00040   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00041   for (i = 0; i < 50; i++)
00042     if (erand48_r (buf, &data, &d) != 0 || d < 0.0 || d >= 1.0)
00043       {
00044         printf ("erand48_r %d %g\n", i, d);
00045         ret = 1;
00046       }
00047 
00048   srand48 ((long int) t);
00049   for (i = 0; i < 50; i++)
00050     if ((l = lrand48 ()) < 0 || l > INT32_MAX)
00051       {
00052         printf ("lrand48 %d %ld\n", i, l);
00053         ret = 1;
00054       }
00055 
00056   srand48_r ((long int) t, &data);
00057   for (i = 0; i < 50; i++)
00058     if (lrand48_r (&data, &l) != 0 || l < 0 || l > INT32_MAX)
00059       {
00060         printf ("lrand48_r %d %ld\n", i, l);
00061         ret = 1;
00062       }
00063 
00064   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00065   for (i = 0; i < 50; i++)
00066     if ((l = nrand48 (buf)) < 0 || l > INT32_MAX)
00067       {
00068         printf ("nrand48 %d %ld\n", i, l);
00069         ret = 1;
00070       }
00071 
00072   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00073   for (i = 0; i < 50; i++)
00074     if (nrand48_r (buf, &data, &l) != 0 || l < 0 || l > INT32_MAX)
00075       {
00076         printf ("nrand48_r %d %ld\n", i, l);
00077         ret = 1;
00078       }
00079 
00080   srand48 ((long int) t);
00081   for (i = 0; i < 50; i++)
00082     if ((l = mrand48 ()) < INT32_MIN || l > INT32_MAX)
00083       {
00084         printf ("mrand48 %d %ld\n", i, l);
00085         ret = 1;
00086       }
00087 
00088   srand48_r ((long int) t, &data);
00089   for (i = 0; i < 50; i++)
00090     if (mrand48_r (&data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
00091       {
00092         printf ("mrand48_r %d %ld\n", i, l);
00093         ret = 1;
00094       }
00095 
00096   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00097   for (i = 0; i < 50; i++)
00098     if ((l = jrand48 (buf)) < INT32_MIN || l > INT32_MAX)
00099       {
00100         printf ("jrand48 %d %ld\n", i, l);
00101         ret = 1;
00102       }
00103 
00104   buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
00105   for (i = 0; i < 50; i++)
00106     if (jrand48_r (buf, &data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
00107       {
00108         printf ("jrand48_r %d %ld\n", i, l);
00109         ret = 1;
00110       }
00111 
00112   return ret;
00113 }