# Positive random number

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

1. ### deepakGuest

Hi,

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

Thanks,
Deepak

deepak, Dec 18, 2007

2. ### Richard HeathfieldGuest

deepak said:

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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Dec 18, 2007

3. ### santoshGuest

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
4. ### 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.

, Dec 18, 2007

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>

6. ### CBFalconerGuest

> 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

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
7. ### Jack KleinGuest

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

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
8. ### Keith ThompsonGuest

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
9. ### jaysomeGuest

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
>
>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
10. ### santoshGuest

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
>>
>>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
11. ### Joachim SchmitzGuest

"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
>>>
>>>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? )

Bye, Jojo

Joachim Schmitz, Dec 19, 2007
12. ### Johannes BauerGuest

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

Johannes Bauer, Dec 19, 2007
13. ### peteGuest

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
14. ### Richard HeathfieldGuest

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@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Dec 19, 2007
15. ### SpoonGuest

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
16. ### James KuyperGuest

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
17. ### Richard HeathfieldGuest

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@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Dec 19, 2007
18. ### Johannes BauerGuest

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

Johannes Bauer, Dec 19, 2007
19. ### James KuyperGuest

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
20. ### Johannes BauerGuest

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