www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/12/20:01:46

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Thanks for help but...
Date: Tue, 12 May 1998 19:12:13 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 42
Message-ID: <3558D74D.4F4A@cs.com>
References: <01bd7da4$0ed9b860$LocalHost AT default>
NNTP-Posting-Host: ppp128.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Daleg wrote:
> 
> i would like to make a function what randomizes songs at start up
> i got 3 sings in datafile but i dont know how to make a good function,
> i did try to use rand, IF, and else but i dont like them, it's not what i
> want

If you want to generate random numbers, you should first seed the random
number generator with a value that's not predictable.  One of the best
values to use is the system clock.  Therefore, a bit of code might look
like this:

#include <stdlib.h>
#include <time.h>

#define NUM_SONGS 3

int main( void )
{
    int song;

    srand( (int) time( NULL ) );
    song = rand() % NUM_SONGS;

    /* your code here */

    return 0;
}

If you have any questions, please feel free to ask.  I should also point
out that rand() is not a very good RNG if all you want is the lower
bits; for really good random numbers you should use random()/srandom().

hth

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Autocracy is based on the assumption|
|       aka Fighteer I       | that one man is wiser than a million |
|   mailto:fighteer AT cs DOT com   | men.  Let's play that over again,    |
| http://www.cs.com/fighteer | too.  Who decides?"   - Lazarus Long |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019