Ralph Cook

Ranch Hand

Posts: 479

posted 6 years ago

To find out what Math.random() does, you look up on

[url]http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html

[/url]

and hunt down the random() method to find the following:

So the statement "double x = Math.random();" calls the (static) method random of the Math class, returning a value of type double that is between 0.0 and 1.0. It is a random or psuedorandom value, i.e., you do not know what it will be from call to call.

The expression "x < myProbDeath" compares the value of x to a variable named myProbDeath, which I presume is a probability that a plant will die. If you set myprobDeath to 30.0, then 30% of your plants will die on executing this code, assuming a truly random number and a big enough population.

rc

[url]http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html

[/url]

and hunt down the random() method to find the following:

`static double random()`

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

So the statement "double x = Math.random();" calls the (static) method random of the Math class, returning a value of type double that is between 0.0 and 1.0. It is a random or psuedorandom value, i.e., you do not know what it will be from call to call.

The expression "x < myProbDeath" compares the value of x to a variable named myProbDeath, which I presume is a probability that a plant will die. If you set myprobDeath to 30.0, then 30% of your plants will die on executing this code, assuming a truly random number and a big enough population.

rc

Tim Hoang

Greenhorn

Posts: 26

posted 6 years ago

Thanks for clearing up the question.

I just looked at it right after you posted and it was really dumb of me to ask.

What I got from the your explanation is that: if x is greater (meaning its chance of living) is less than its chance of dying(myProbDeath).

I didn't catch that.

btw I want to know your time in responding to this question because I am learning java by myself and I am not sure how slow I am picking up this subject.

I just looked at it right after you posted and it was really dumb of me to ask.

What I got from the your explanation is that: if x is greater (meaning its chance of living) is less than its chance of dying(myProbDeath).

I didn't catch that.

btw I want to know your time in responding to this question because I am learning java by myself and I am not sure how slow I am picking up this subject.

Tim Hoang

Greenhorn

Posts: 26

Matthew Brown

Bartender

Posts: 4568

9

posted 6 years ago

That just means "does

`strA.compareTo(strB)`return a value less than zero". Again, the thing to do is to check the documentation to see what the return value of`compareTo`means.`compareTo`is part of the`Comparable`interface, and is used to be able to sort objects in order.
Campbell Ritchie

Sheriff

Posts: 54087

130

posted 6 years ago

That's not what it means at all. It means you are choosing a "random" number between 0 and 0.9999999999999... and comparing another number to it.has exactly a 25% chance of being executed and exactly a 75% chance of not being executed, assuming myRandom is uniformly distributed across that range. Try counting, with 0, 0.05, 0.1, 0.15 ... 0.95 (not 1.0), and see what happens. See how many of those numbers will allow execution of theTim Hoang wrote: . . . if x is greater (meaning its chance of living) is less than its chance of dying(myProbDeath). . . .

`if`. Note thatwill have a very very slightly lower chance than 25%, because you are using the range from 0.7500000000000001... to 0.9999999999999..., not to 1.0. Try counting with 0, 0.05, 0.1 ... 0.95 again, and see what happens.

Tim Hoang

Greenhorn

Posts: 26

posted 6 years ago

Thank you Campbell Ritchie,

So I think I found an error in the book.

strA = "TOMATO"

strB="tomato"

strC ="tom"

which of the following is true?

answer:

I thought only equals method comes out true or false and compareTo produces values depending on the string precedence.

So I think I found an error in the book.

strA = "TOMATO"

strB="tomato"

strC ="tom"

which of the following is true?

answer:

I thought only equals method comes out true or false and compareTo produces values depending on the string precedence.

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

Not quite. compareTo() does produce values - int values - based on string precedence. And contrary to what the comment says, it produces a negative value in this case. But you need to look at the whole expression:

This is equivalent to

which is

which is

which is

Tim Hoang wrote:I thought only equals method comes out true or false and compareTo produces values depending on the string precedence.

Not quite. compareTo() does produce values - int values - based on string precedence. And contrary to what the comment says, it produces a negative value in this case. But you need to look at the whole expression:

This is equivalent to

which is

which is

which is