#22 Random playlists segfault on Solaris

closed-fixed
Joe Drew
5
2002-12-09
2002-12-03
Philip Kendall
No

Attempting to use random playlists on Solaris produces
a segfault on startup. The problem is the lines in
playlist.c which do

pl->numfiles * ((double)random()/RAND_MAX)

On Solaris, random() returns "successive pseudo-random
numbers in the range from 0 to 2**31-1" rather than
between 0 and RAND_MAX (which is 32767, at least on
this machine:

$ uname -a
SunOS cass45 5.8 Generic_108528-14 sun4u sparc
SUNW,Ultra-5_10

). One fix is to change all the three lines using
RAND_MAX to do

random() % pl->numfiles

which will work, but may not be ideal if random() is a
pRNG which doesn't produce good low-order bits.

Discussion

  • Joe Drew
    Joe Drew
    2002-12-09

    Logged In: YES
    user_id=330927

    I assume rand() returns between 0 and RAND_MAX, so I've
    modified all uses of random() to rand() and srandom() to
    srand() in CVS.

    If you like, you can check out the current CVS version of
    mpg321 - let me know if anything breaks.

     
  • Joe Drew
    Joe Drew
    2002-12-09

    • status: open --> closed-fixed
     
  • Philip Kendall
    Philip Kendall
    2002-12-10

    Logged In: YES
    user_id=29214

    I don't think that fix is quite right; lines like

    i = (pl->numfiles-1) * ((double)rand()/RAND_MAX);

    are going to select the last file in the playlist only on
    the rare occasion that rand() returns precisely RAND_MAX;
    something like

    i = pl->numfiles * (rand()/(RAND_MAX+1.0));

    should work. Apologies if this is rubbish as I can't get the
    CVS version to compile due to problems with interactions
    between the different versions of automake I have installed