Positive random number

D

deepak

Hi,

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

Thanks,
Deepak
 
R

Richard Heathfield

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?
 
D

divyank.rastogi

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
 
T

Tor Rustad

deepak said:
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.
 
C

CBFalconer

Tor said:
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
 
J

Jack Klein

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
 
K

Keith Thompson

Jack Klein said:
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.
 
J

jaysome

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" :^)
 
S

santosh

jaysome said:
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.
 
J

Joachim Schmitz

santosh said:
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
 
P

pete

Johannes said:
Keith said:
Jack Klein said:
On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
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.
 
R

Richard Heathfield

pete said:
Johannes said:
Keith said:
On Tue, 18 Dec 2007 00:43:10 -0800 (PST), deepak
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.
 
S

Spoon

pete said:
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.
 
R

Richard Heathfield

Spoon said:
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.
 
J

Johannes Bauer

Richard said:
Greater than zero.


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
 
J

James Kuyper

Johannes said:
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.
 
J

Johannes Bauer

James said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top