This is the mail archive of the cygwin mailing list for the Cygwin 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]

RE: drand48() (and erand48) returns only zeros


> -----Original Message-----
> From: cygwin-owner On Behalf Of meadmaker1066-cyg
> Sent: 12 November 2004 15:42

> drand48 and erand48 return only 0.0 no matter how many
> times I call them. The code works fine on the Linux
> computers at school, and the compiler does not report
> any errors or warnings.
> 

Found this comment in newlib/libc/stdlib/rand48.c

 	144	Note that all three methods of seeding the random number
generator
 	145	always also set the multiplicand and addend for any of the six
 	146	generator calls.

Which somewhat implies that if you *don't* seed the rng, you get zero for
multiplicand and addend.  And indeed:


dk@mace /test/rand> cat randtest.cc
#include <stdlib.h>
#include <cmath>

#include <iostream>
using namespace std;

int main (int argc, const char **argv)
{
  if (argc > 1)
  {
  long int seed;
    if (1 != sscanf (argv[1], "%i", &seed))
    {
      printf ("failure!\n");
      return 0;
    }
    printf ("Seeding rng with %x\n", seed);
    srand48 (seed);
  }

  for(int i=0; i<10; i++)
    printf("%f\n",drand48());
  return 0;
}

dk@mace /test/rand> g++ randtest.cc -o randtest
dk@mace /test/rand> ./randtest.exe
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
dk@mace /test/rand> ./randtest.exe
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
dk@mace /test/rand>
dk@mace /test/rand> ./randtest.exe 100
Seeding rng with 64
0.251059
0.208948
0.940928
0.422546
0.395893
0.382565
0.231731
0.535547
0.533210
0.862949
dk@mace /test/rand> ./randtest.exe 101
Seeding rng with 65
0.121861
0.913539
0.679373
0.888066
0.384079
0.598532
0.727126
0.157215
0.409803
0.484128
dk@mace /test/rand> ./randtest.exe 100
Seeding rng with 64
0.251059
0.208948
0.940928
0.422546
0.395893
0.382565
0.231731
0.535547
0.533210
0.862949


    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


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