public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jan Dvorak <johnydog@go.cz>
To: Joachim Bauernberger <nuklear@singnet.com.sg>
Cc: help-gcc@gnu.org
Subject: Re: gcc newbie: rand()
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.10.9912101334290.28392-100000@ns.go.cz> (raw)
Message-ID: <19991231222400.-bc8K6bBP17gGHa4q9K9SDQcXZt0zBh9EspYER-GJ3c@z> (raw)
In-Reply-To: <3850EBFA.BE9086DC@singnet.com.sg>

On Fri, 10 Dec 1999, 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 <stdlib.h>
> #include <stdio.h>
> 
> #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!
> */
> 

Yes, it's hard to believe, but even books can be wrong :) Your book
complains about Borland C compiler. This compiler has limit for generating
random numbers between 0 and 32768, so that's what was '/32768.0' for. GCC
uses number which is defined by RAND_MAX so, after correction that line
should be:

		x = rand() /RAND_MAX;

anyway, please refer to rand() man pages, because random number
generating is very complex theme.

Happy coding!
Jan Dvorak

 

  reply	other threads:[~1999-12-31 22:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-10  4:04 Joachim Bauernberger
1999-12-10  4:40 ` Jan Dvorak [this message]
1999-12-31 22:24   ` Jan Dvorak
1999-12-10 22:18 ` Martin Kahlert
1999-12-31 22:24   ` Martin Kahlert
1999-12-12 14:41 ` Patrick Block
1999-12-31 22:24   ` Patrick Block
1999-12-12 15:29 ` Rick Dearman
1999-12-31 22:24   ` Rick Dearman
1999-12-12 15:35 ` Rick Dearman
1999-12-31 22:24   ` Rick Dearman
1999-12-31 22:24 ` Joachim Bauernberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.10.9912101334290.28392-100000@ns.go.cz \
    --to=johnydog@go.cz \
    --cc=help-gcc@gnu.org \
    --cc=nuklear@singnet.com.sg \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).