Random Numbers?

Discussion in 'C Programming' started by Kyle, Jan 13, 2004.

  1. Kyle

    Kyle Guest

    Hello,
    I'm trying to get a random number using C. I've tried using random(),
    but my program keeps producing numbers that are way out of range from
    the numbers I put in.

    Anyone got any suggestions?

    Kyle
    --
    comp.lang.c.moderated - moderation address:
    Kyle, Jan 13, 2004
    #1
    1. Advertising

  2. Kyle

    Artie Gold Guest

    Kyle wrote:
    > Hello,
    > I'm trying to get a random number using C. I've tried using random(),
    > but my program keeps producing numbers that are way out of range from
    > the numbers I put in.
    >


    See, for example:

    http://www.eskimo.com/~scs/C-faq/q13.16.html

    (as well as other nearby entries in the FAQ).

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    Artie Gold, Jan 13, 2004
    #2
    1. Advertising

  3. Kyle

    illecebra Guest

    On 13 Jan 2004 02:40:40 GMT, (Kyle) wrote:

    >Hello,
    >I'm trying to get a random number using C. I've tried using random(),
    >but my program keeps producing numbers that are way out of range from
    >the numbers I put in.
    >
    >Anyone got any suggestions?
    >
    >Kyle


    Use the modulus operator (%) to bring the number in range.

    Susan
    --
    comp.lang.c.moderated - moderation address:
    illecebra, Jan 14, 2004
    #3
  4. (Kyle) writes:
    > I'm trying to get a random number using C. I've tried using random(),
    > but my program keeps producing numbers that are way out of range from
    > the numbers I put in.
    >
    > Anyone got any suggestions?


    The multiplication on line 76 of your program should be a division.
    And you misspelled "onomatopoeia" in the comment on line 103.

    I'm just guessing, of course, since you didn't post anything that
    would let us figure out what the actual problem might be.

    There is no function in standard C called random(). The standard
    defines a rand() function (whose implementation is typically not very
    good, unfortunately), so using your system's random() function might
    give you better results. I presume you've read the documentation for
    the random() function. It probably returns numbers in a wide range;
    you'll need to do something to scale them to the range you want.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    --
    comp.lang.c.moderated - moderation address:
    Keith Thompson, Jan 14, 2004
    #4
  5. "Kyle" <> wrote in message
    news:...
    > Hello,
    > I'm trying to get a random number using C. I've tried using random(),
    > but my program keeps producing numbers that are way out of range from
    > the numbers I put in.
    >
    > Anyone got any suggestions?


    FAQ, FAQ, FAQ...!

    http://www.eskimo.com/~scs/C-faq/top.html

    Look up Q 13.16
    --
    comp.lang.c.moderated - moderation address:
    Peter Pichler, Jan 14, 2004
    #5
  6. In article <>, Kyle <>
    writes
    >Hello,
    >I'm trying to get a random number using C. I've tried using random(),
    >but my program keeps producing numbers that are way out of range from
    >the numbers I put in.
    >
    >Anyone got any suggestions?


    Yes, try telling us exactly what it is that you want, starting with
    whether you need a genuinely random number (int or double) or a
    pseudo-random number.


    --
    Francis Glassborow ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    --
    comp.lang.c.moderated - moderation address:
    Francis Glassborow, Jan 14, 2004
    #6
  7. In comp.lang.c.moderated Kyle <> wrote:
    # Hello,
    # I'm trying to get a random number using C. I've tried using random(),
    # but my program keeps producing numbers that are way out of range from
    # the numbers I put in.

    So you do something like calling random(34)? That is not how random()
    works.

    # Anyone got any suggestions?

    Read about how random works, what header declares its prototype and how
    to turn on all warnings of your compiler. You would have been told your
    error if you only had done this.

    Once you have done that, the other part of your question is answered in
    the clc FAQ.

    Regards,

    Jens
    --
    Jens Schweikhardt http://www.schweikhardt.net/
    SIGSIG -- signature too long (core dumped)
    --
    comp.lang.c.moderated - moderation address:
    Jens Schweikhardt, Jan 14, 2004
    #7
  8. Kyle

    Micah Cowan Guest

    (Kyle) writes:

    > Hello,
    > I'm trying to get a random number using C. I've tried using random(),
    > but my program keeps producing numbers that are way out of range from
    > the numbers I put in.
    >
    > Anyone got any suggestions?
    >
    > Kyle


    Well, first off, random() isn't a C function; it's BSD
    originally. The C version is rand() (which will return an int,
    not long int like random()).

    Second, neither random() nor rand() allow you to "put in"
    numbers. Look it up in your documentation. It will always return
    a value between 0 and an implementation-defined quantity,
    RAND_MAX. To use rand() to produce integers in the range from x
    to y, inclusive, you should use something like:

    int result = x+(int) (y*rand()/(RAND_MAX+1.0));

    Make sure that you #include <stdlib.h>; and also that you seed
    the generator using srand(), unless you want the same sequence of
    numbers every time you run.

    --
    Micah J. Cowan

    --
    comp.lang.c.moderated - moderation address:
    Micah Cowan, Jan 14, 2004
    #8
  9. Kyle

    John L Guest

    "Micah Cowan" <> wrote in message news:...
    > (Kyle) writes:
    >
    >
    > Well, first off, random() isn't a C function; it's BSD
    > originally.


    It may not be specified as part of ISO C but it is stretching
    to deny it is a C function. Of course, we know what you mean
    but this illustrates a problem with some of the "OT" warnings
    we see in clc.

    John.
    John L, Jan 14, 2004
    #9
  10. Kyle

    CBFalconer Guest

    John L wrote:
    > "Micah Cowan" <> wrote
    > > (Kyle) writes:
    > >
    > > Well, first off, random() isn't a C function; it's BSD
    > > originally.

    >
    > It may not be specified as part of ISO C but it is stretching
    > to deny it is a C function. Of course, we know what you mean
    > but this illustrates a problem with some of the "OT" warnings
    > we see in clc.


    It is not specified in the standard. That means that what it does
    and how it should be called is up to the user. To use it in this
    group means you should also publish all the source for random(),
    else it is impossible to evaluate that use.

    int random(int param)
    {
    return(puts("I don't feel like working today\n", stdout));
    }

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jan 14, 2004
    #10
  11. Kyle

    Tom St Denis Guest

    "CBFalconer" <> wrote in message
    news:...
    > John L wrote:
    > > "Micah Cowan" <> wrote
    > > > (Kyle) writes:
    > > >
    > > > Well, first off, random() isn't a C function; it's BSD
    > > > originally.

    > >
    > > It may not be specified as part of ISO C but it is stretching
    > > to deny it is a C function. Of course, we know what you mean
    > > but this illustrates a problem with some of the "OT" warnings
    > > we see in clc.

    >
    > It is not specified in the standard. That means that what it does
    > and how it should be called is up to the user. To use it in this
    > group means you should also publish all the source for random(),
    > else it is impossible to evaluate that use.
    >
    > int random(int param)
    > {
    > return(puts("I don't feel like working today\n", stdout));
    > }


    puts() doesn't take an FILE* argument. Also "puts" adds a newline so you
    don't need one.

    Lame script kiddies... when will y'all learn to use a man page or two... If
    you're going to be a smartarse about your comment at least be right.

    Tom
    Tom St Denis, Jan 14, 2004
    #11
  12. Kyle

    Grumble Guest

    Tom St Denis wrote:

    > puts() doesn't take an FILE* argument. Also "puts" adds a newline
    > so you don't need one.
    >
    > Lame script kiddies... when will y'all learn to use a man page or
    > two... If you're going to be a smartarse about your comment at
    > least be right.


    Tom,

    Don't be a moron, you know he meant fputs(). It's just a typo.
    Grumble, Jan 14, 2004
    #12
  13. Kyle

    CBFalconer Guest

    Grumble wrote:
    >
    > Tom St Denis wrote:
    >
    > > puts() doesn't take an FILE* argument. Also "puts" adds a newline
    > > so you don't need one.
    > >
    > > Lame script kiddies... when will y'all learn to use a man page or
    > > two... If you're going to be a smartarse about your comment at
    > > least be right.

    >
    > Tom,
    >
    > Don't be a moron, you know he meant fputs(). It's just a typo.


    I experimentally unplonked him this morning. 3 out of 3 messages
    from him have no raison d'etre. Back in the PLONK file.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jan 14, 2004
    #13
  14. Kyle

    Thad Smith Guest

    Micah Cowan wrote:

    > Well, first off, random() isn't a C function; it's BSD
    > originally.


    I wouldn't say that random() isn't a C function, since it can be
    implemented in C. It just isn't part of the Standard C library.

    > To use rand() to produce integers in the range from x
    > to y, inclusive, you should use something like:
    >
    > int result = x+(int) (y*rand()/(RAND_MAX+1.0));


    That code has three problems:
    1. If overflow were not a concern, it would compute a result from x to
    x+y-1, inclusive, not x to y.
    2. It is likely to overflow, producing undefined results.
    3. The distribution is uneven for large values of (y-x).

    To get the range x to y, inclusive, the following approximates even
    distribution when r=RAND_MAX/(y-x+1) is large.

    int result = x+(int) ((y-x+1)*(double)rand()/(RAND_MAX+1.0));

    The difference in distribution of different integers in the range x to
    y, is, in general, r:(r+1), assuming that the distribution or rand() is
    perfectly flat from 0 to RAND_MAX. To improve this, an iterative scheme
    can be used which only scales rand() results which are in the range
    0..(floor(RAND_MAX/(y-x+1))*(y-x+1)). Other values are either discarded
    and rand() called again, or somehow used with a subsequent rand() result
    to approximate a level distribution.

    Thad
    --
    comp.lang.c.moderated - moderation address:
    Thad Smith, Jan 17, 2004
    #14
  15. Kyle

    Jerry Coffin Guest

    In article <>, says...
    > Hello,
    > I'm trying to get a random number using C. I've tried using random(),
    > but my program keeps producing numbers that are way out of range from
    > the numbers I put in.
    >
    > Anyone got any suggestions?


    You've gotten a lot of suggestions. IMO, of what you've received so
    far, Thad Smith's post is probably the most thorough and accurate. I'll
    add only one detail: he alluded to code that discards values as needed
    to produce a flat distribution, but didn't include such code in his
    post. Here's some code to do that:

    /* return a pseudo-random number between 0 and limit inclusive.
    */
    int rand_lim(int limit) {
    int divisor = RAND_MAX/(limit+1);
    int retval;

    do {
    retval = rand() / divisor;
    } while (retval > limit);

    return retval;
    }

    If you want to specify both a lower and an upper limit, you can call
    this specifying the difference between the two, and then add the result
    to the lower limit you've specified -- I have code for that, but it's
    written as a C++ function that overloads this one, so it isn't topical
    here.

    Also note that although this is written as a loop, you can usually
    expect the loop to execute only once per function call, and chances of
    it executing more than twice in a particular call are almost
    astronomically remote (unless rand() has massive defects).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    --
    comp.lang.c.moderated - moderation address:
    Jerry Coffin, Jan 24, 2004
    #15
    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. globalrev
    Replies:
    4
    Views:
    753
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    657
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    216
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    249
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,152
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page