Why will this not generate a random number!?

Discussion in 'C Programming' started by John Cassidy, Oct 27, 2004.

  1. John Cassidy

    John Cassidy Guest

    This has been driving me crazy. I've done basic C in school, but my
    education is mainly based on object oriented design theory where Java
    is our tool. For some reason, while helping a friend with a C
    Programming lab. I cannot for the life of me generate a random number.
    i don't know what is wrong. please help.

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

    #define RAND_MAX 32768

    int main(void)
    {
    float b;

    b = ((float)rand()/RAND_MAX);


    printf("Random Number: %f", b);

    system("PAUSE");
    return 0;
    }
     
    John Cassidy, Oct 27, 2004
    #1
    1. Advertising

  2. John Cassidy wrote:
    > This has been driving me crazy. I've done basic C in school, but my
    > education is mainly based on object oriented design theory where Java
    > is our tool. For some reason, while helping a friend with a C
    > Programming lab. I cannot for the life of me generate a random number.
    > i don't know what is wrong. please help.


    rand() will generate spesific "random", se my addition to the code below.

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > #define RAND_MAX 32768
    >
    > int main(void)
    > {
    > float b;


    // The function srand() is used to seed the random sequence generated by
    // rand(). For any given seed, rand() will generate a specific "random"
    // sequence over and over again.
    srand(time(NULL));

    > b = ((float)rand()/RAND_MAX);
    >
    >
    > printf("Random Number: %f", b);
    >
    > system("PAUSE");
    > return 0;
    > }


    If you're on a UNIX system you might want to consider using /dev/urandom
    to generate random.

    --
    Regards
    Hans-Christian Egtvedt
     
    Hans-Christian Egtvedt, Oct 27, 2004
    #2
    1. Advertising

  3. (John Cassidy) writes:
    > This has been driving me crazy. I've done basic C in school, but my
    > education is mainly based on object oriented design theory where Java
    > is our tool. For some reason, while helping a friend with a C
    > Programming lab. I cannot for the life of me generate a random number.
    > i don't know what is wrong. please help.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > #define RAND_MAX 32768
    >
    > int main(void)
    > {
    > float b;
    >
    > b = ((float)rand()/RAND_MAX);
    >
    >
    > printf("Random Number: %f", b);
    >
    > system("PAUSE");
    > return 0;
    > }


    Thank you for posting a complete program (too many people post
    fragments or pseudo-code). But a little more detail on what behavior
    you're actually seeing would be helpful. You say it doesn't generate
    a random number; what does it do?

    You don't need <math.h>; rand() is declared in <stdlib.h>

    Don't define RAND_MAX yourself, it's defined for you in <stdlib.h>.

    Your output doesn't end in a newline; on systems, this might cause
    your output not to appear at all. system("PAUSE") won't work if the
    system doesn't have a PAUSE command.

    Your system should have documentation on the rand() and srand()
    functions. Find it and read it.

    I think your actual problem is probably answered by entry 13.17 of the
    C FAQ, <http://www.eskimo.com/~scs/C-faq/q13.17.html>. Questions
    13.15 through 13.20 deal with random numbers. Read the whole section.
    Read the whole FAQ.

    If you call rand() without first calling srand(), it's equivalent to
    calling rand() after calling srand(1). This always gives you the same
    sequence of numbers. On one system I just tried, the first call to
    rand() gave me 0.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 27, 2004
    #3
  4. John Cassidy wrote:

    > This has been driving me crazy. I've done basic C in school, but my
    > education is mainly based on object oriented design theory where Java
    > is our tool. For some reason, while helping a friend with a C
    > Programming lab. I cannot for the life of me generate a random number.
    > i don't know what is wrong. please help.


    Before we get started here, please note that this is fully covered in
    the FAQ, and it is normally accepted practice in newsgroups to check the
    FAQ before posting. Before you shout, "But I didn't know there was a
    FAQ," note also that it is normally accepted practice to lurk in
    newsgroups before posting; many people here have links to the FAQ in
    their sigs and, if you had lurked for anything like the expected period,
    you would have seen one of the periodic postings of the FAQ itself.

    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > #define RAND_MAX 32768


    No, you don't do this. RAND_MAX is a macro defined by your implementation.

    >
    > int main(void)
    > {
    > float b;
    >


    You ought to, unless you want the same sequence everytime you run the
    program, seed the random number generator once with srand() before using
    it. See the FAQ for details.

    > b = ((float)rand()/RAND_MAX);


    OK, so you want b in the range [0,1]. Generally, however, you really
    want b in the range [0,1):
    b = rand()/(RAND_MAX+1.);

    > printf("Random Number: %f", b);
    >
    > system("PAUSE");


    You are getter off not relying on the host system having a command named
    "PAUSE".
    getchar();
    would accomplish the same thing, wouldn't it?
    > return 0;
    > }
     
    Martin Ambuhl, Oct 27, 2004
    #4
  5. (John Cassidy) wrote in message news:<>...
    > This has been driving me crazy. I've done basic C in school, but my
    > education is mainly based on object oriented design theory where Java
    > is our tool. For some reason, while helping a friend with a C
    > Programming lab. I cannot for the life of me generate a random number.
    > i don't know what is wrong. please help.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > #define RAND_MAX 32768
    >
    > int main(void)
    > {
    > float b;
    >
    > b = ((float)rand()/RAND_MAX);
    >
    >
    > printf("Random Number: %f", b);
    >
    > system("PAUSE");
    > return 0;
    > }


    Try to seed with srand()
     
    Stuart Gerchick, Oct 27, 2004
    #5
  6. John Cassidy

    Dan Pop Guest

    In <> (John Cassidy) writes:

    >This has been driving me crazy. I've done basic C in school, but my
    >education is mainly based on object oriented design theory where Java
    >is our tool. For some reason, while helping a friend with a C
    >Programming lab. I cannot for the life of me generate a random number.
    >i don't know what is wrong. please help.
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <math.h>


    What did you include <math.h> for?

    >#define RAND_MAX 32768


    If your compiler didn't complain about the redefinition of RAND_MAX, get
    a better one. If it did, you have absolutely no excuse for having
    ignored it!

    >int main(void)
    >{
    > float b;
    >
    > b = ((float)rand()/RAND_MAX);
    >
    >
    > printf("Random Number: %f", b);


    Why do you think this number is not random enough? If you want different
    numbers on different runs, open your favourite C book and read the
    specification of the rand() and srand() functions. Or read the FAQ,
    which is a MUST before posting for the first time, anyway.

    > system("PAUSE");


    You don't need this crap. If you're using a poorly designed IDE that
    closes the program console as soon as the program terminates, getchar()
    is the function you want to call here. It is also supposed to fix the
    bug in your printf call, which is not terminating the line properly.

    > return 0;
    >}


    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 27, 2004
    #6
  7. John Cassidy

    Dan Pop Guest

    In <> Martin Ambuhl <> writes:

    >John Cassidy wrote:
    >
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <math.h>
    >>
    >> #define RAND_MAX 32768

    >
    >No, you don't do this. RAND_MAX is a macro defined by your implementation.
    >
    >>
    >> int main(void)
    >> {
    >> float b;
    >>

    >
    >You ought to, unless you want the same sequence everytime you run the
    >program, seed the random number generator once with srand() before using
    >it. See the FAQ for details.
    >
    >> b = ((float)rand()/RAND_MAX);

    >
    >OK, so you want b in the range [0,1]. Generally, however, you really
    >want b in the range [0,1):
    > b = rand()/(RAND_MAX+1.);


    Have a closer look at his definition of RAND_MAX. If his implementation
    is using a 15-bit generator (like the one shown in the C standard), he is
    effectively dividing by RAND_MAX+1 :)

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 27, 2004
    #7
  8. John Cassidy

    CBFalconer Guest

    Stuart Gerchick wrote:
    > (John Cassidy) wrote in message
    >
    >> This has been driving me crazy. I've done basic C in school, but
    >> my education is mainly based on object oriented design theory
    >> where Java is our tool. For some reason, while helping a friend
    >> with a C Programming lab. I cannot for the life of me generate a
    >> random number. i don't know what is wrong. please help.
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <math.h>
    >>
    >> #define RAND_MAX 32768
    >>
    >> int main(void)
    >> {
    >> float b;
    >>
    >> b = ((float)rand()/RAND_MAX);
    >> printf("Random Number: %f", b);
    >> system("PAUSE");
    >> return 0;
    >> }

    >
    > Try to seed with srand()


    Maybe because he should be using double, not float?

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 27, 2004
    #8
  9. John Cassidy

    Old Wolf Guest

    Martin Ambuhl <> wrote:
    > John Cassidy wrote:
    >
    > > system("PAUSE");

    >
    > You are getter off not relying on the host system having a
    > command named "PAUSE".
    > getchar();
    > would accomplish the same thing, wouldn't it?


    Not entirely: "pause" waits for any keypress, whereas getchar()
    usually requires the Enter key.
     
    Old Wolf, Oct 28, 2004
    #9
  10. Old Wolf wrote:

    > Martin Ambuhl <> wrote:
    >
    >>John Cassidy wrote:
    >>
    >>
    >>> system("PAUSE");

    >>
    >>You are getter off not relying on the host system having a
    >>command named "PAUSE".
    >> getchar();
    >>would accomplish the same thing, wouldn't it?

    >
    >
    > Not entirely: "pause" waits for any keypress, whereas getchar()
    > usually requires the Enter key.


    Can you really predict with confidence that "pause" works that way on
    any platform?
     
    Martin Ambuhl, Oct 28, 2004
    #10
  11. John Cassidy

    Michael Guest

    "Dan Pop" <> wrote in message
    news:clolrj$mi3$...
    > In <>

    (John Cassidy) writes:
    >
    > >This has been driving me crazy. I've done basic C in school, but my
    > >education is mainly based on object oriented design theory where Java
    > >is our tool. For some reason, while helping a friend with a C
    > >Programming lab. I cannot for the life of me generate a random number.
    > >i don't know what is wrong. please help.
    > >
    > >#include <stdio.h>
    > >#include <stdlib.h>
    > >#include <math.h>

    >
    > What did you include <math.h> for?
    >
    > >#define RAND_MAX 32768

    >
    > If your compiler didn't complain about the redefinition of RAND_MAX, get
    > a better one. If it did, you have absolutely no excuse for having
    > ignored it!
    >
    > >int main(void)
    > >{
    > > float b;
    > >
    > > b = ((float)rand()/RAND_MAX);
    > >
    > >
    > > printf("Random Number: %f", b);

    >
    > Why do you think this number is not random enough? If you want different
    > numbers on different runs, open your favourite C book and read the
    > specification of the rand() and srand() functions. Or read the FAQ,
    > which is a MUST before posting for the first time, anyway.
    >
    > > system("PAUSE");

    >
    > You don't need this crap. If you're using a poorly designed IDE that
    > closes the program console as soon as the program terminates, getchar()
    > is the function you want to call here. It is also supposed to fix the
    > bug in your printf call, which is not terminating the line properly.
    >

    Please elaborate further on the last line. What is printf supposed to do?
    how should printf terminate the line?
    Thanks
    Mike

    > > return 0;
    > >}

    >
    > Dan
    > --
    > Dan Pop
    > DESY Zeuthen, RZ group
    > Email:
    > Currently looking for a job in the European Union
     
    Michael, Oct 28, 2004
    #11
  12. >This has been driving me crazy. I've done basic C in school, but my
    >education is mainly based on object oriented design theory where Java
    >is our tool. For some reason, while helping a friend with a C
    >Programming lab. I cannot for the life of me generate a random number.


    rand() does not generate random numbers. It generates pseudo-random
    numbers. The difference may not mean much to you in your application,
    but for serious users of cryptography (e.g. spies), it can mean the
    difference between life and death.

    >i don't know what is wrong. please help.
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <math.h>
    >
    >#define RAND_MAX 32768


    You do not get to define RAND_MAX. The implementation does that.

    >
    >int main(void)
    >{
    > float b;
    >
    > b = ((float)rand()/RAND_MAX);
    >
    >
    > printf("Random Number: %f", b);


    How do you know the result you got is NOT pseudo-random?

    rand() is defined to generate consistent sequences if you
    are concerned with getting the same result on repeated invocations.
    In that case, look up srand().

    >
    > system("PAUSE");

    PAUSE: command not found
    > return 0;
    >}


    Gordon L. Burditt
     
    Gordon Burditt, Oct 28, 2004
    #12
  13. John Cassidy

    Michael Mair Guest

    Michael wrote:
    > "Dan Pop" <> wrote in message
    > news:clolrj$mi3$...
    >
    >>In <>

    >
    > (John Cassidy) writes:
    >

    [snip]
    >>> printf("Random Number: %f", b);

    printf("Random Number: %f\n", b);

    [snip]
    >>> system("PAUSE");

    >>
    >>You don't need this crap. If you're using a poorly designed IDE that
    >>closes the program console as soon as the program terminates, getchar()
    >>is the function you want to call here. It is also supposed to fix the
    >>bug in your printf call, which is not terminating the line properly.
    >>

    >
    > Please elaborate further on the last line. What is printf supposed to do?
    > how should printf terminate the line?


    The last output of your program should throw out a linebreak ('\n',
    see the above correction of the original code).
    Otherwise, it may be that you will not see the output (which also
    could be achieved by playing with the prompt) nor actually get it.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
     
    Michael Mair, Oct 28, 2004
    #13
  14. John Cassidy

    Dan Pop Guest

    In <> (Old Wolf) writes:

    >Martin Ambuhl <> wrote:
    >> John Cassidy wrote:
    >>
    >> > system("PAUSE");

    >>
    >> You are getter off not relying on the host system having a
    >> command named "PAUSE".
    >> getchar();
    >> would accomplish the same thing, wouldn't it?

    >
    >Not entirely: "pause" waits for any keypress, whereas getchar()
    >usually requires the Enter key.


    Is it really *any* key, or only *some* keys? If the latter, then a
    function waiting for a well defined key is preferable.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 28, 2004
    #14
  15. CBFalconer <> writes:
    > Stuart Gerchick wrote:
    >> (John Cassidy) wrote in message
    >>
    >>> This has been driving me crazy. I've done basic C in school, but
    >>> my education is mainly based on object oriented design theory
    >>> where Java is our tool. For some reason, while helping a friend
    >>> with a C Programming lab. I cannot for the life of me generate a
    >>> random number. i don't know what is wrong. please help.
    >>>
    >>> #include <stdio.h>
    >>> #include <stdlib.h>
    >>> #include <math.h>
    >>>
    >>> #define RAND_MAX 32768
    >>>
    >>> int main(void)
    >>> {
    >>> float b;
    >>>
    >>> b = ((float)rand()/RAND_MAX);
    >>> printf("Random Number: %f", b);
    >>> system("PAUSE");
    >>> return 0;
    >>> }

    >>
    >> Try to seed with srand()

    >
    > Maybe because he should be using double, not float?


    Um, no. Double would only give you more precision (with a number that
    has only 15 bits of precision in the first place). The argument to
    printf() is promoted to double anyway, so there's no type conflict
    with "%f" format.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 28, 2004
    #15
  16. John Cassidy

    CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> Stuart Gerchick wrote:
    >>> (John Cassidy) wrote in message
    >>>
    >>>> This has been driving me crazy. I've done basic C in school, but
    >>>> my education is mainly based on object oriented design theory
    >>>> where Java is our tool. For some reason, while helping a friend
    >>>> with a C Programming lab. I cannot for the life of me generate a
    >>>> random number. i don't know what is wrong. please help.
    >>>>
    >>>> #include <stdio.h>
    >>>> #include <stdlib.h>
    >>>> #include <math.h>
    >>>>
    >>>> #define RAND_MAX 32768
    >>>>
    >>>> int main(void)
    >>>> {
    >>>> float b;
    >>>>
    >>>> b = ((float)rand()/RAND_MAX);
    >>>> printf("Random Number: %f", b);
    >>>> system("PAUSE");
    >>>> return 0;
    >>>> }
    >>>
    >>> Try to seed with srand()

    >>
    >> Maybe because he should be using double, not float?

    >
    > Um, no. Double would only give you more precision (with a number that
    > has only 15 bits of precision in the first place). The argument to
    > printf() is promoted to double anyway, so there's no type conflict
    > with "%f" format.


    You're right. I missed the fact that he is generating only one
    random value from the initial state.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 28, 2004
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    24
    Views:
    9,368
    Malcolm
    Oct 18, 2004
  2. RayKnight

    Generate a random number

    RayKnight, Apr 29, 2005, in forum: Java
    Replies:
    2
    Views:
    327
    Peter MacMillan
    Apr 29, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,966
    Smokey Grindel
    Dec 2, 2006
  4. globalrev
    Replies:
    4
    Views:
    771
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,175
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page