Positive random number

Discussion in 'C Programming' started by deepak, Dec 18, 2007.

  1. deepak

    deepak Guest

    Hi,

    Can someone give the standard function which can create positive
    integer value in C?

    Thanks,
    Deepak
     
    deepak, Dec 18, 2007
    #1
    1. Advertising

  2. deepak said:

    > Hi,
    >
    > Can someone give the standard function which can create positive
    > integer value in C?


    What does your textbook say about the generation of random numbers?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 18, 2007
    #2
    1. Advertising

  3. deepak

    santosh Guest

    deepak wrote:

    > Hi,
    >
    > Can someone give the standard function which can create positive
    > integer value in C?
    >
    > Thanks,
    > Deepak


    Look-up rand.
     
    santosh, Dec 18, 2007
    #3
  4. deepak

    Guest

    On Dec 18, 4:27 am, santosh <> wrote:
    > deepak wrote:
    > > Hi,

    >
    > > Can someone give the standard function which can create positive
    > > integer value in C?

    >
    > > Thanks,
    > > Deepak

    >
    > Look-up rand.


    You may want to man for rand series entirely; e.g. you may want to use
    srand, and seed it with something like your system time to generate
    random sequences, etc.

    :D
     
    , Dec 18, 2007
    #4
  5. deepak

    Tor Rustad Guest

    deepak wrote:
    > Hi,
    >
    > Can someone give the standard function which can create positive
    > integer value in C?


    There is no standard C function that creates numbers out of nothing.

    If you are rather looking for a pseudo-random generator, see the C FAQ.

    --
    Tor < | tr i-za-h a-z>
     
    Tor Rustad, Dec 18, 2007
    #5
  6. deepak

    CBFalconer Guest

    Tor Rustad wrote:
    > deepak wrote:
    >
    >> Can someone give the standard function which can create positive
    >> integer value in C?

    >
    > There is no standard C function that creates numbers out of nothing.


    However, the sequence "i = N;" is fairly safe, as long as N
    represents a sequence of numeric digits (not including the '-'
    sign) which represents a value <= INT_MAX. This even has the
    elegant characteristic of allowing you to pick your own integer
    value (within broad limits).

    Another sophisticated sequence (which is not always possible) is:

    if (i < 0) i = -i;

    HTH

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee.
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 19, 2007
    #6
  7. deepak

    Jack Klein Guest

    On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    <> wrote in comp.lang.c:

    > Hi,
    >
    > Can someone give the standard function which can create positive
    > integer value in C?


    Other's have talked about "rand()", but I don't see anything in your
    post that requires it. Here's a function guaranteed to meet the
    requirement you asked for in the body of your message:

    int create_positive_integer_value_in_C(void)
    {
    return 42;
    }

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 19, 2007
    #7
  8. Jack Klein <> writes:
    > On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    > <> wrote in comp.lang.c:
    >> Can someone give the standard function which can create positive
    >> integer value in C?

    >
    > Other's have talked about "rand()", but I don't see anything in your
    > post that requires it.

    [...]

    The subject was "Positive random number". That information should
    have been in the body of the original post.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 19, 2007
    #8
  9. deepak

    jaysome Guest

    On Tue, 18 Dec 2007 20:30:18 -0600, Jack Klein <>
    wrote:

    >On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    ><> wrote in comp.lang.c:
    >
    >> Hi,
    >>
    >> Can someone give the standard function which can create positive
    >> integer value in C?

    >
    >Other's have talked about "rand()", but I don't see anything in your
    >post that requires it. Here's a function guaranteed to meet the
    >requirement you asked for in the body of your message:
    >
    >int create_positive_integer_value_in_C(void)
    >{
    > return 42;
    >}


    Definitely *not* guaranteed.

    In C99, section 5.2.4.1 Translation limits:

    "The implementation shall be able to translate and execute at least
    one program that contains at least one instance of every one of the
    following limits:

    ....

    31 significant initial characters in an external identifier..."

    The identifier "create_positive_integer_value_in_C" is an external
    identifier with 34 characters, and thus exceeds the minimum number of
    characters *guaranteed* to be accepted by the standard. In C90, the
    minimum was a paltry six characters.

    Now, an identifier such as "create_positive_int_value_in_C" (30
    characters) is acceptable in C99, but not necessarily in C90, though
    arguably it is acceptable in most--if not all--C90 compilers in the
    real world (for that matter, so is
    "create_positive_integer_value_in_C" :^)

    --
    jay
     
    jaysome, Dec 19, 2007
    #9
  10. deepak

    santosh Guest

    jaysome wrote:

    > On Tue, 18 Dec 2007 20:30:18 -0600, Jack Klein <>
    > wrote:
    >
    >>On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    >><> wrote in comp.lang.c:
    >>
    >>> Hi,
    >>>
    >>> Can someone give the standard function which can create positive
    >>> integer value in C?

    >>
    >>Other's have talked about "rand()", but I don't see anything in your
    >>post that requires it. Here's a function guaranteed to meet the
    >>requirement you asked for in the body of your message:
    >>
    >>int create_positive_integer_value_in_C(void)
    >>{
    >> return 42;
    >>}

    >
    > Definitely *not* guaranteed.
    >
    > In C99, section 5.2.4.1 Translation limits:
    >
    > "The implementation shall be able to translate and execute at least
    > one program that contains at least one instance of every one of the
    > following limits:
    >
    > ...
    >
    > 31 significant initial characters in an external identifier..."
    >
    > The identifier "create_positive_integer_value_in_C" is an external
    > identifier with 34 characters, and thus exceeds the minimum number of
    > characters *guaranteed* to be accepted by the standard. In C90, the
    > minimum was a paltry six characters.


    What part of "31 significant initial characters" did you not understand.
    Jack's function name will only cause problems if he happened to have
    defined another identifier with the same sequence of 31 initial
    characters.
     
    santosh, Dec 19, 2007
    #10
  11. "santosh" <> schrieb im Newsbeitrag
    news:fkakhl$nr5$...
    > jaysome wrote:
    >
    >> On Tue, 18 Dec 2007 20:30:18 -0600, Jack Klein <>
    >> wrote:
    >>
    >>>On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    >>><> wrote in comp.lang.c:
    >>>
    >>>> Hi,
    >>>>
    >>>> Can someone give the standard function which can create positive
    >>>> integer value in C?
    >>>
    >>>Other's have talked about "rand()", but I don't see anything in your
    >>>post that requires it. Here's a function guaranteed to meet the
    >>>requirement you asked for in the body of your message:
    >>>
    >>>int create_positive_integer_value_in_C(void)
    >>>{
    >>> return 42;
    >>>}

    >>
    >> Definitely *not* guaranteed.
    >>
    >> In C99, section 5.2.4.1 Translation limits:
    >>
    >> "The implementation shall be able to translate and execute at least
    >> one program that contains at least one instance of every one of the
    >> following limits:
    >>
    >> ...
    >>
    >> 31 significant initial characters in an external identifier..."
    >>
    >> The identifier "create_positive_integer_value_in_C" is an external
    >> identifier with 34 characters, and thus exceeds the minimum number of
    >> characters *guaranteed* to be accepted by the standard. In C90, the
    >> minimum was a paltry six characters.

    >
    > What part of "31 significant initial characters" did you not understand.
    > Jack's function name will only cause problems if he happened to have
    > defined another identifier with the same sequence of 31 initial
    > characters.

    What part of "Definitely *not* guaranteed" didn't you understand? :cool:)

    Bye, Jojo
     
    Joachim Schmitz, Dec 19, 2007
    #11
  12. Keith Thompson schrieb:
    > Jack Klein <> writes:
    >> On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    >> <> wrote in comp.lang.c:
    >>> Can someone give the standard function which can create positive
    >>> integer value in C?

    >> Other's have talked about "rand()", but I don't see anything in your
    >> post that requires it.

    > [...]
    >
    > The subject was "Positive random number". That information should
    > have been in the body of the original post.


    http://xkcd.org/221/

    :))

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Dec 19, 2007
    #12
  13. deepak

    pete Guest

    Johannes Bauer wrote:
    >
    > Keith Thompson schrieb:
    > > Jack Klein <> writes:
    > >> On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    > >> <> wrote in comp.lang.c:
    > >>> Can someone give the standard function which can create positive
    > >>> integer value in C?
    > >> Other's have talked about "rand()",
    > >> but I don't see anything in your
    > >> post that requires it.

    > > [...]
    > >
    > > The subject was "Positive random number". That information should
    > > have been in the body of the original post.


    Th return value of rand isn't guaranteed to be positive.

    --
    pete
     
    pete, Dec 19, 2007
    #13
  14. pete said:

    > Johannes Bauer wrote:
    >>
    >> Keith Thompson schrieb:
    >> > Jack Klein <> writes:
    >> >> On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
    >> >> <> wrote in comp.lang.c:
    >> >>> Can someone give the standard function which can create positive
    >> >>> integer value in C?
    >> >> Other's have talked about "rand()",
    >> >> but I don't see anything in your
    >> >> post that requires it.
    >> > [...]
    >> >
    >> > The subject was "Positive random number". That information should
    >> > have been in the body of the original post.

    >
    > Th return value of rand isn't guaranteed to be positive.


    Nor is it guaranteed to be random.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 19, 2007
    #14
  15. deepak

    Spoon Guest

    pete wrote:

    > The return value of rand isn't guaranteed to be positive.


    What does "positive" mean? Is x positive if and only if x > 0?
    Does that mean that 0 is neither positive nor negative?

    For the record, the rand function computes a sequence of
    pseudo-random integers in the range 0 to RAND_MAX.
     
    Spoon, Dec 19, 2007
    #15
  16. deepak

    James Kuyper Guest

    Spoon wrote:
    > pete wrote:
    >
    >> The return value of rand isn't guaranteed to be positive.

    >
    > What does "positive" mean? Is x positive if and only if x > 0?
    > Does that mean that 0 is neither positive nor negative?


    Correct.
     
    James Kuyper, Dec 19, 2007
    #16
  17. Spoon said:

    > pete wrote:
    >
    >> The return value of rand isn't guaranteed to be positive.

    >
    > What does "positive" mean?


    Greater than zero.

    > Is x positive if and only if x > 0?


    Yes.

    > Does that mean that 0 is neither positive nor negative?


    Yes.

    > For the record, the rand function computes a sequence of
    > pseudo-random integers in the range 0 to RAND_MAX.


    Yes.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 19, 2007
    #17
  18. Richard Heathfield schrieb:

    >>> The return value of rand isn't guaranteed to be positive.

    >> What does "positive" mean?

    >
    > Greater than zero.
    >
    >> Is x positive if and only if x > 0?

    >
    > Yes.
    >
    >> Does that mean that 0 is neither positive nor negative?

    >
    > Yes.


    The question if 0 is positive or not is one which has long been debated
    my mathematicians around the world - and still is. Many are of the
    opinion that it is useful to define it as positive, just as it is useful
    to define 0^0 = 1 (although this is not as clear as it might seem).

    The point is: It's a question a definition. Your answer is much too
    dogmatic.

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Dec 19, 2007
    #18
  19. deepak

    James Kuyper Guest

    Johannes Bauer wrote:
    > Richard Heathfield schrieb:
    >
    >>>> The return value of rand isn't guaranteed to be positive.
    >>> What does "positive" mean?

    >> Greater than zero.
    >>
    >>> Is x positive if and only if x > 0?

    >> Yes.
    >>
    >>> Does that mean that 0 is neither positive nor negative?

    >> Yes.

    >
    > The question if 0 is positive or not is one which has long been debated
    > my mathematicians around the world - and still is. Many are of the
    > opinion that it is useful to define it as positive, just as it is useful
    > to define 0^0 = 1 (although this is not as clear as it might seem).
    >
    > The point is: It's a question a definition. Your answer is much too
    > dogmatic.


    There might be obscure discussions among mathematicians in which such a
    definition is used, but I believe that in almost all contexts, the
    overwhelming majority of the mathematically literate population consider
    0 to be neither positive nor negative. I don't think it's excessively
    dogmatic to insist on interpreting it that way in this context.
     
    James Kuyper, Dec 19, 2007
    #19
  20. James Kuyper schrieb:

    >> The point is: It's a question a definition. Your answer is much too
    >> dogmatic.

    >
    > There might be obscure discussions among mathematicians in which such a
    > definition is used, but I believe that in almost all contexts, the
    > overwhelming majority of the mathematically literate population consider
    > 0 to be neither positive nor negative. I don't think it's excessively
    > dogmatic to insist on interpreting it that way in this context.


    They are not obscure. Consider the work of Peano
    (http://en.wikipedia.org/wiki/Giuseppe_Peano) which in his older works
    state that the positive Integers start at 1, while at a later release
    (Peano.G.: Formulaire de mathématiques 5 Bde. Turin, Bocca 1895-1908) he
    states they start at zero.

    It is *not* something that "almost all mathematicians" agree about, it
    is primarily a question of usefulness. Both variants are common, it even
    depends which university you're attending. Dogmatism are stupid, there
    are good reasons why zero should be considered a positive integer and
    there are also good reasons why it shouldn't. It's important to base
    your decision on reason, not on "that's what I think everybody is doing".

    Then again - in a trueley mathematic sense - almost all mathematicians
    consider zero to be nonpositive. Almost all of them agree that zero is a
    positive number, too. This is because "almost" in a mathematic sense
    means "except for a finite number of exceptions" :)

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Dec 19, 2007
    #20
    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:
    774
    Gabriel Genellina
    Apr 20, 2008
  2. EK
    Replies:
    0
    Views:
    320
  3. MRAB
    Replies:
    2
    Views:
    311
    John Machin
    Jan 31, 2009
  4. Hicham Mouline
    Replies:
    2
    Views:
    778
    Keith Thompson
    Apr 23, 2010
  5. VK
    Replies:
    15
    Views:
    1,179
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page