public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc newbie: rand()
@ 1999-12-10  4:04 Joachim Bauernberger
  1999-12-10  4:40 ` Jan Dvorak
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Joachim Bauernberger @ 1999-12-10  4:04 UTC (permalink / raw)
  To: help-gcc

/*  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!
*/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
@ 1999-12-10  4:40 ` Jan Dvorak
  1999-12-31 22:24   ` Jan Dvorak
  1999-12-10 22:18 ` Martin Kahlert
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Jan Dvorak @ 1999-12-10  4:40 UTC (permalink / raw)
  To: Joachim Bauernberger; +Cc: help-gcc

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

 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
  1999-12-10  4:40 ` Jan Dvorak
@ 1999-12-10 22:18 ` Martin Kahlert
  1999-12-31 22:24   ` Martin Kahlert
  1999-12-12 14:41 ` Patrick Block
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Martin Kahlert @ 1999-12-10 22:18 UTC (permalink / raw)
  To: help-gcc

In article < 3850EBFA.BE9086DC@singnet.com.sg >,
	Joachim Bauernberger <nuklear@singnet.com.sg> writes:
> /*  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?

It's wrong because of the strange value 32768. Use
x = rand() / (double) RAND_MAX;
instead.

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
  1999-12-10  4:40 ` Jan Dvorak
  1999-12-10 22:18 ` Martin Kahlert
@ 1999-12-12 14:41 ` Patrick Block
  1999-12-31 22:24   ` Patrick Block
  1999-12-12 15:29 ` Rick Dearman
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Patrick Block @ 1999-12-12 14:41 UTC (permalink / raw)
  To: help-gcc

Joachim,

Firstly, don't assume that because it's in a book it's correct! Some books
are definitely better than others.

The best book on C (IMHO) is The C Programming Language by K&R, and on p252
of that book the ANSI C standard prototype for rand() is given...

int rand (void)
rand returns a pseudo-random integer in the range 0 to RAND_MAX, which is at
least 32767.

The book you have does not give the best way for getting a specific random
number. Your book's version only works if RAND_MAX is less than 32768, which
it isn't necessarily. I think your version will work if you replace the
magic number 32768 with RAND_MAX.

If you use modulus operator, however, you can create a more elegant formula.
This is the idiom that most C programmers use as far as I'm aware:

int result;
result = (rand () % 6) + 1;

...That should do it!

I'm also pasting an example program that demonstrates one way to get a
random number. This program compiles fine with mingw32 gcc. Here's the way I
compiled it:

    gcc -luser32 -o getrand.exe getrand.c

If you're using gcc in linux or if you're using djgpp, you can omit the
"-luser32" and it should work fine.

/***-----
|
| getrand.c
|
| Demonstrates how to generate a random
| number between 1 and a given value.
|
| NOTE: In real use, you would not want
| to call srand() before every call to
| rand(), because if you call rand()
| quickly in succession, it will keep
| returning the same results. Instead,
| move the call to srand to the beginning
| of your program and only call it once.
|
| It works in this program, but only
| because getrand() is called a single
| time.
|
|
-----***/


/*** preprocessor commands ----------------------------------***/

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


/*** global variables ---------------------------------------***/


/*** function prototypes ------------------------------------***/

int getrand (int toprange);


/*** program functions --------------------------------------***/

/*>>>_____ FUNCTION: getrand _____ */

 /***-----
 |
 | get a random # between
 | 1 and toprange
 |
 -----***/

int getrand (toprange)
{
 srand (time (NULL));
 return (rand () % toprange) + 1;
}


/*>>>_____ FUNCTION: main _____ */

 /***-----
 |
 | get toprange from
 | command line, get
 | and display a random
 | number between 1 &
 | toprange
 |
 -----***/

int main (int argc, char *argv [])
{
 int toprange = 0;

 if (argc == 2)
  toprange = atoi (argv [1]);
 if (! toprange)
 {
  printf ("\nProper usage:\n\n"
    "%s #\n"
    "Where # is positive number that "
    "represents the top value for "
    "the returned random number.",
    argv [0]);
  return 1;
 }
 printf ("\n\nRandom number between 1 & %d:\t%d",
  toprange,
  getrand (toprange));
 return 0;
}



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
                   ` (2 preceding siblings ...)
  1999-12-12 14:41 ` 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 ` Joachim Bauernberger
  5 siblings, 1 reply; 12+ messages in thread
From: Rick Dearman @ 1999-12-12 15:29 UTC (permalink / raw)
  To: help-gcc

I think this is what you want:

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


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 <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!
> */

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
                   ` (3 preceding siblings ...)
  1999-12-12 15:29 ` Rick Dearman
@ 1999-12-12 15:35 ` Rick Dearman
  1999-12-31 22:24   ` Rick Dearman
  1999-12-31 22:24 ` Joachim Bauernberger
  5 siblings, 1 reply; 12+ messages in thread
From: Rick Dearman @ 1999-12-12 15:35 UTC (permalink / raw)
  To: help-gcc

Try this:

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

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 = (float)( get_rand(5) / 32768.0 );
  n = 1 + (int) (6*x);
	
  printf("x = %d\n", n);

  return 0;

}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-12 15:29 ` Rick Dearman
@ 1999-12-31 22:24   ` Rick Dearman
  0 siblings, 0 replies; 12+ messages in thread
From: Rick Dearman @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

I think this is what you want:

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


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 <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!
> */

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10  4:40 ` Jan Dvorak
@ 1999-12-31 22:24   ` Jan Dvorak
  0 siblings, 0 replies; 12+ messages in thread
From: Jan Dvorak @ 1999-12-31 22:24 UTC (permalink / raw)
  To: Joachim Bauernberger; +Cc: help-gcc

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

 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* gcc newbie: rand()
  1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
                   ` (4 preceding siblings ...)
  1999-12-12 15:35 ` Rick Dearman
@ 1999-12-31 22:24 ` Joachim Bauernberger
  5 siblings, 0 replies; 12+ messages in thread
From: Joachim Bauernberger @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

/*  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!
*/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-10 22:18 ` Martin Kahlert
@ 1999-12-31 22:24   ` Martin Kahlert
  0 siblings, 0 replies; 12+ messages in thread
From: Martin Kahlert @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

In article < 3850EBFA.BE9086DC@singnet.com.sg >,
	Joachim Bauernberger <nuklear@singnet.com.sg> writes:
> /*  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?

It's wrong because of the strange value 32768. Use
x = rand() / (double) RAND_MAX;
instead.

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-12 15:35 ` Rick Dearman
@ 1999-12-31 22:24   ` Rick Dearman
  0 siblings, 0 replies; 12+ messages in thread
From: Rick Dearman @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

Try this:

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

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 = (float)( get_rand(5) / 32768.0 );
  n = 1 + (int) (6*x);
	
  printf("x = %d\n", n);

  return 0;

}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: gcc newbie: rand()
  1999-12-12 14:41 ` Patrick Block
@ 1999-12-31 22:24   ` Patrick Block
  0 siblings, 0 replies; 12+ messages in thread
From: Patrick Block @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

Joachim,

Firstly, don't assume that because it's in a book it's correct! Some books
are definitely better than others.

The best book on C (IMHO) is The C Programming Language by K&R, and on p252
of that book the ANSI C standard prototype for rand() is given...

int rand (void)
rand returns a pseudo-random integer in the range 0 to RAND_MAX, which is at
least 32767.

The book you have does not give the best way for getting a specific random
number. Your book's version only works if RAND_MAX is less than 32768, which
it isn't necessarily. I think your version will work if you replace the
magic number 32768 with RAND_MAX.

If you use modulus operator, however, you can create a more elegant formula.
This is the idiom that most C programmers use as far as I'm aware:

int result;
result = (rand () % 6) + 1;

...That should do it!

I'm also pasting an example program that demonstrates one way to get a
random number. This program compiles fine with mingw32 gcc. Here's the way I
compiled it:

    gcc -luser32 -o getrand.exe getrand.c

If you're using gcc in linux or if you're using djgpp, you can omit the
"-luser32" and it should work fine.

/***-----
|
| getrand.c
|
| Demonstrates how to generate a random
| number between 1 and a given value.
|
| NOTE: In real use, you would not want
| to call srand() before every call to
| rand(), because if you call rand()
| quickly in succession, it will keep
| returning the same results. Instead,
| move the call to srand to the beginning
| of your program and only call it once.
|
| It works in this program, but only
| because getrand() is called a single
| time.
|
|
-----***/


/*** preprocessor commands ----------------------------------***/

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


/*** global variables ---------------------------------------***/


/*** function prototypes ------------------------------------***/

int getrand (int toprange);


/*** program functions --------------------------------------***/

/*>>>_____ FUNCTION: getrand _____ */

 /***-----
 |
 | get a random # between
 | 1 and toprange
 |
 -----***/

int getrand (toprange)
{
 srand (time (NULL));
 return (rand () % toprange) + 1;
}


/*>>>_____ FUNCTION: main _____ */

 /***-----
 |
 | get toprange from
 | command line, get
 | and display a random
 | number between 1 &
 | toprange
 |
 -----***/

int main (int argc, char *argv [])
{
 int toprange = 0;

 if (argc == 2)
  toprange = atoi (argv [1]);
 if (! toprange)
 {
  printf ("\nProper usage:\n\n"
    "%s #\n"
    "Where # is positive number that "
    "represents the top value for "
    "the returned random number.",
    argv [0]);
  return 1;
 }
 printf ("\n\nRandom number between 1 & %d:\t%d",
  toprange,
  getrand (toprange));
 return 0;
}



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1999-12-31 22:24 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-10  4:04 gcc newbie: rand() Joachim Bauernberger
1999-12-10  4:40 ` Jan Dvorak
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

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).