Skip to content

[Question]What does that command means?

-migrated-

6 posts

[Question]What does that command means?

Posted Oct 3, 2010 4:39 pm

Hi,

In the source code of the flash game I saw:

Code: [Select]

this.score = this.score - Math.random() * 5;
this.score = this.score < 0 ? (0) : (this.score);
The first line substracts old value of score and Random number from 0 to 4 (am I right?). But what second line does?

Re: [Question]What does that command means?

Posted Oct 3, 2010 5:44 pm

That's a stupid way to write an if-statement. It means something like this:
If(this.score < 0) this.score = 0;
else this.score = this.score;

It's just making sure you don't get a negative score

Re: [Question]What does that command means?

Posted Oct 3, 2010 6:01 pm

Ok, thx for your reply. Now I understand this, but what about the first line... Math.random()*5 means a random number from 0 to 4, or from 1 to 5?

Re: [Question]What does that command means?

Posted Oct 3, 2010 7:51 pm

I don't know the differences between flash and C/C++, but in C/C++ that would generate a random number and multiply it by five. If that actually means mod, it would generate a random number from 0~4

Re: [Question]What does that command means?

Posted Oct 3, 2010 8:10 pm

Math.random()
Pseudo-random number between 0 and 1 (0 <= n < 1).

Math.random() * i
Pseudo-random number between 0 and i (0 <= n < i).

Math.random() * 5
Pseudo-random number between 0 and 5 (0 <= n < 5).

I assume score is an integer, so it automatically rounds it.