Random Variable help! (newbie question) 
Author Message
 Random Variable help! (newbie question)

Hello.

I am learning C and the book I have (Leran C on the Macintosh) doesn't
explain some things very clearly.  I am having dificulty with random
variables.

#include <stdio.h>

int main( void )
{
   int foo;
   foo = RollOne();
   printf( "%d", foo );

Quote:
}

int RollOne( int )
{
   return (rand() %6) + 1;

Quote:
}

Shouldn't this work?  Please help!!

Thanks,

Peter



Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)



Quote:
> Hello.

> I am learning C and the book I have (Leran C on the Macintosh)
doesn't
> explain some things very clearly.  I am having dificulty with
random
> variables.

> #include <stdio.h>

> int main( void )
> {
>    int foo;
>    foo = RollOne();
>    printf( "%d", foo );
> }

> int RollOne( int )
> {
>    return (rand() %6) + 1;
> }

> Shouldn't this work?  Please help!!

> Thanks,

> Peter

Hi Peter,

I wish you had said what it did wrong.  If you don't get any
output at all, try adding a '\n' in the printf statement.  If
you don't like the randomness of the number you are getting,
there are three things I can suggest.

1. Make sure you include stdlib.h, where rand() is defined.

2. If you always get the same numbers every time you run the
program, look at the srand() function, also in stdlib.h.

3. Get the FAQ for comp.lang.c at
www.eskimo.com/~scs/C-faq/top.html, it has a section which
explains ways to get random numbers to do what you want.

The FAQ has links to other good information about programming in
C, you might find one that you like better than your book.

Jack



Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:

> Hello.

> I am learning C and the book I have (Leran C on the Macintosh) doesn't
> explain some things very clearly.  I am having dificulty with random
> variables.

> #include <stdio.h>

> int main( void )
> {
>    int foo;
>    foo = RollOne();
>    printf( "%d", foo );
> }

> int RollOne( int )
> {
>    return (rand() %6) + 1;
> }

> Shouldn't this work?  Please help!!

Before making a call to rand() you must 'seed' it by making a call to
srand().  Programmers often use the current number oc clock ticks for
the random seed to insure that each series obtained from rand() is
different.  For more information on rand() and srand() try some of these
`net resources:
C Programming `Net Resources
C-FAQ ftp sites: ftp://ftp.eskimo.com
                 ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
http://www.cit.ac.nz/smac/cprogram/cstart.htm
http://www.angelfire.com/sc/electron/
http://www.cm.cf.ac.uk/Dave/C/CE.html
http://www.lysator.liu.se/c/
http://www-isis.ecs.soton.ac.uk/computing/c/notes/tao.html
http://www.cast.msstate.edu/~billy/c-prog.html

--

Education is a companion which no misfortune can depress,
no crime can destroy, and no enemy can alienate...
at home a friend, abroad an introduction,
in solitude a solace, and in society and ornament.
                                Joseph Addison.
************************************************

You can't use void main() I have a patent on it!

************************************************

*    Remove the _JUNK_ when replying to me.    *
************************************************



Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:

>#include <stdio.h>

>int main( void )
>{
>   int foo;
>   foo = RollOne();

There is no RollOne() function in C.

Quote:
>   printf( "%d", foo );
>}

>int RollOne( int )

You appear to want:

   int RollOne( void )

To use the function in main(), either place the function or a
prototype before the definition of main().

Quote:
>{
>   return (rand() %6) + 1;

This is not a very good method (see the FAQ).  Also, it's a
good idea to declare a function before using it.  In this
case, rand() is declared in stdlib.h.
Quote:
>}



Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

On Sun, 12 Oct 1997 11:44:11 -0400, Alicia Carla

Quote:

>Before making a call to rand() you must 'seed' it by
>making a call to srand().

That's not true.  While you MAY use srand(), you are
not required to do so.  If you don't, the code will
behave as if you included an srand( 1 ) near the
beginning of execution.


Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:


> >#include <stdio.h>

> >int main( void )
> >{
> >   int foo;
> >   foo = RollOne();

> There is no RollOne() function in C.

Of course there is he just wrote it.  Oh wait a minute, I understand,
you just missspoke yourself, you meant to say "There is no RollOne()
function in the Standard C Library."  although why this is of any
importance whatsoever is beyond me.  Or are you suggesting that the only
function allowed in posts to comp.lang.c are main() and the functions in
the ANSI/ISO Standard Library.  If that is the case then this forum is
becoming a little too limited to be of any use.

Quote:
> >   printf( "%d", foo );
> >}

> >int RollOne( int )

> You appear to want:

>    int RollOne( void )

> To use the function in main(), either place the function or a
> prototype before the definition of main().

> >{
> >   return (rand() %6) + 1;

> This is not a very good method (see the FAQ).  Also, it's a
> good idea to declare a function before using it.  In this
> case, rand() is declared in stdlib.h.

> >}

--
A definition is embodied by what it defines.
A definition that defines nothing is not a definition.
A definition that has no example, defines nothing.
A definition is designed to facilitate communication.
*********************************************************
Man is still the most extraordinary computer of all.
                                    John F. Kennedy
*********************************************************

*        Remove the _JUNK_ when replying to me.         *
*********************************************************
You can't use void main() I have a patent on it!


Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:



>> >#include <stdio.h>

>> >int main( void )
>> >{
>> >   int foo;
>> >   foo = RollOne();

>> There is no RollOne() function in C.

>Of course there is he just wrote it.

Its declaration is not in scope.  As I pointed out, a prototype
could be included before main() of the whole function could be
moved before main().


Thu, 30 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:




>>> >#include <stdio.h>

>>> >int main( void )
>>> >{
>>> >   int foo;
>>> >   foo = RollOne();

>>> There is no RollOne() function in C.

>>Of course there is he just wrote it.

>Its declaration is not in scope.  As I pointed out, a prototype
>could be included before main() of the whole function could be
>moved before main().

I don't see what your point is.

#include<stdio.h>
int main(void)
    {
    int i;
    i = get_i();
    printf("%d\n", i);
    return 0;
    }

int get_i(void)
    {
    return 1;
    }

There is nothing wrong with that. If you include the function
definition, it's not some mysterious function any more. Was it
the fact that if a prototype was in scope it would have alerted
the compiler that RollOne wasn't called with an int that your
remark refered to? That I can see.

--
Craig

Manchester, NH
Maybe a great magnet pulls/All souls towards truth
Or maybe it is life itself/That feeds wisdom/To its
youth.   -- k.d. lang



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:




>>> >#include <stdio.h>

>>> >int main( void )
>>> >{
>>> >   int foo;
>>> >   foo = RollOne();

>>> There is no RollOne() function in C.

>>Of course there is he just wrote it.

>Its declaration is not in scope.  As I pointed out, a prototype
>could be included before main() of the whole function could be
>moved before main().

I don't see what your point is.

#include<stdio.h>
int main(void)
    {
    int i;
    i = get_i();
    printf("%d\n", i);
    return 0;
    }

int get_i(void)
    {
    return 1;
    }

There is nothing wrong with that. If you include the function
definition, it's not some mysterious function any more. Was it
the fact that if a prototype was in scope it would have alerted
the compiler that RollOne wasn't called with an int that your
remark refered to? That I can see.

--
Craig

Manchester, NH
Maybe a great magnet pulls/All souls towards truth
Or maybe it is life itself/That feeds wisdom/To its
youth.   -- k.d. lang



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:




> >> >#include <stdio.h>

> >> >int main( void )
> >> >{
> >> >   int foo;
> >> >   foo = RollOne();

> >> There is no RollOne() function in C.

> >Of course there is he just wrote it.

> Its declaration is not in scope.  As I pointed out, a prototype
> could be included before main() of the whole function could be
> moved before main().

Then you should have just said that the function needed a declaration to
be in scope. Actually, under the rules of C, if a function is called
without a prototype or definition in scope the compiler assumes it is a
function returning int with the given arguments.  since RollOne() is a
function returning an int with no arguments this will compile just
fine.  Although given the posters, admitted, newbie status, your
reference to having a prototype or definition in scope was correct.

Just don't say it is not C.

--
A definition is embodied by what it defines.
A definition that defines nothing is not a definition.
A definition that has no example, defines nothing.
A definition is designed to facilitate communication.
*********************************************************
Man is still the most extraordinary computer of all.
                                    John F. Kennedy
*********************************************************

*        Remove the _JUNK_ when replying to me.         *
*********************************************************
You can't use void main() I have a patent on it!



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:



> >Hello.

> >I am learning C and the book I have (Leran C on the Macintosh) doesn't
> >explain some things very clearly.  I am having dificulty with random
> >variables.

> >#include <stdio.h>

> >int RollOne( int )
> >{
> >   return (rand() %6) + 1;
> >}
> /*aside from rollone taking an int and not using it, and return not
> having parantheses i can't see anything wrong.

You imply that both things are wrong, which is not true. It is not
required to use the parameters of a function and it is *not* required
to put parantheses around the return expression.

Quote:
> did you include hte random header file?

There is no special random header file, but you are partly right:
he forgot to include <stdlib.h> (this does not change anything
here though).

Quote:
> also, place rollone above the main program*/

A recommendable thing to do, but not mandatory, because a prototype
for "RollOne" is not required.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:

> Hello.

> I am learning C and the book I have (Leran C on the Macintosh) doesn't
> explain some things very clearly.  I am having dificulty with random
> variables.

> #include <stdio.h>

> int main( void )
> {
>    int foo;
>    foo = RollOne();
>    printf( "%d", foo );
> }

> int RollOne( int )
> {
>    return (rand() %6) + 1;
> }

> Shouldn't this work?  Please help!!

Hi Red Foxx,

It's always good to give an explicite explanation of what "this does
not work" means. In your case I con only spot two obvious errors,
one of which will should prevent you from compiling the code:
  1) All parameters in a function definition must be named. Simply
     stating "int" is not supported by ANSI-C (this is an error and
     should stop the compiler)
  2) You do not include <stdlib.h>, which contains the prototype
     for the "rand()" function.

Also you should think about seeding your random number generator
to get a unique random number each time you start your program and
that "%" is a poor choice for getting a random number in a certain
range. The c.l.c FAQ covers these topics in greater detail, please
look for:
  13.16:  How can I get random integers in a certain range?
  13.17:  Each time I run my program, I get the same sequence of numbers
          back from rand().

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)


Quote:

>>On Sun, 12 Oct 1997 17:36:56 -0400, Alicia Carla Longstreet



>>>> >#include <stdio.h>

>>>> >int main( void )
>>>> >{
>>>> >   int foo;
>>>> >   foo = RollOne();

>>>> There is no RollOne() function in C.

>>>Of course there is he just wrote it.

>>Its declaration is not in scope.  As I pointed out, a prototype
>>could be included before main() of the whole function could be
>>moved before main().

>I don't see what your point is.

I don't know what else to say :-(

Perhaps an example will break through.  Suppose the function takes
a double.  By using an implicit declaration, a call to the
function with a parameter as a literal integer (e.g. func( 25 )),
the parameter will not be converted to double.

Explicit declaration is safer.



Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)



Quote:
>Hello.

>I am learning C and the book I have (Leran C on the Macintosh) doesn't
>explain some things very clearly.  I am having dificulty with random
>variables.

>#include <stdio.h>

>int main( void )
>{
>   int foo;
>   foo = RollOne();
>   printf( "%d", foo );
>}

>int RollOne( int )
>{
>   return (rand() %6) + 1;
>}

/*aside from rollone taking an int and not using it, and return not
having parantheses i can't see anything wrong. did you include hte
random header file? also, place rollone above the main program*/


Fri, 31 Mar 2000 03:00:00 GMT  
 Random Variable help! (newbie question)

Quote:

> >#include <stdio.h>

> >int main( void )
> >{
> >   int foo;
> >   foo = RollOne();
> >   printf( "%d", foo );
> >}

> >int RollOne( int )
> >{
> >   return (rand() %6) + 1;
> >}
> /*aside from rollone taking an int and not using it, and return not
> having parantheses i can't see anything wrong. did you include hte
> random header file? also, place rollone above the main program*/

Not using an argument is not an error.  The error is defining the
function as taking an int and not sending it one.

There is no requirement that the argument to return have parens.

There is no "random" header.  The rand function is in stdlib.h.
Even so, in this case it is a good idea but not critical.

Why should rollone be above the main function?  Try to answer
without using the word "should."

--

http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.



Fri, 31 Mar 2000 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Help! Newbie question on viewing variables in debugger

2. Help needed with random variables

3. Newbie help: :Random Matrix generation in C

4. help with random function in mfc (newbie)

5. Newbie Variable question

6. Newbie C question regarding static & const variables

7. newbie question on evaluating a string variable

8. Global variables (newbie question)

9. Setting a method variable from within a switch statement - Newbie question

10. C newbie question about Variable number of arguments

11. Newbie Question: variable types

12. Global Variables in MFC - Newbie Question

 

 
Powered by phpBB® Forum Software