From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rick Dearman To: help-gcc@gnu.org Subject: Re: gcc newbie: rand() Date: Fri, 31 Dec 1999 22:24:00 -0000 Message-ID: <38542F8E.BC63A5BD@ricken.demon.co.uk> References: <3850EBFA.BE9086DC@singnet.com.sg> X-SW-Source: 1999-12n/msg00192.html Message-ID: <19991231222400.s3m6d9kg7ekQBH3fEl_ftGAOhTnu0C9ac3J2aLkL6JE@z> I think this is what you want: #include #include #include int get_rand(int range) { int mrand; mrand = (int)((double)rand() / ((double)RAND_MAX +1) * range); return mrand; } int main(void) { float x; int n; srand((unsigned int)time((time_t *)NULL)); x = get_rand(5); n = 1 + (6*x); printf("x = %d\n", n); return 0; } Joachim Bauernberger wrote: > > /* hi there! > * > * can anybody tell my why the following code compiled with:............ > > * gcc -g -D_GNU_SOURCE filename.c -o outputfile > * > * ....generates all sort of strange numbers but when compiled under > borland gives me the desired * random numbers between 1 and 6 ??? how > do i do it to get the numbers between 1 and 6. > * this example is from a book about C so it can't be that wrong, can > it? > */ > > #include > #include > > #define SEED 12345 > > main() > > { > > float x; > int n; > > srand(SEED); > > x = rand() /32768.0; > n = 1 + (int) (6 * x); > > printf("x = %d", n); > } > > /*Thank's > Joachim! > */